FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » not understanding pattern matching
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
not understanding pattern matching [message #178609] Wed, 11 July 2012 22:35 Go to next message
Michael Joel is currently offline  Michael Joel
Messages: 42
Registered: October 2011
Karma: 0
Member
I am wanting to change words in a string that has special tags.
Ex. "This is a %/i%special%i% word in the string."
If I want to replace any word between the %/i% and %i% what would I do?

I messed with preg_repace but I couldn't understand the pattern system.

Mike
Re: not understanding pattern matching [message #178610 is a reply to message #178609] Wed, 11 July 2012 22:49 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(Michael Joel)

> I am wanting to change words in a string that has special tags.
> Ex. "This is a %/i%special%i% word in the string."
> If I want to replace any word between the %/i% and %i% what would I do?
>
> I messed with preg_repace but I couldn't understand the pattern system.

What have you tried so far? Do you have any experiences with regular
expressions?

Micha

--
http://mfesser.de/
Fotos | Blog | Flohmarkt
Re: not understanding pattern matching [message #178611 is a reply to message #178610] Wed, 11 July 2012 23:11 Go to previous messageGo to next message
Michael Joel is currently offline  Michael Joel
Messages: 42
Registered: October 2011
Karma: 0
Member
Michael Fesser wrote:

> .oO(Michael Joel)
>
>
>> I am wanting to change words in a string that has special tags.
>> Ex. "This is a %/i%special%i% word in the string."
>> If I want to replace any word between the %/i% and %i% what would I do?
>>
>> I messed with preg_repace but I couldn't understand the pattern system.
>
>
> What have you tried so far? Do you have any experiences with regular
> expressions?
>
> Micha
>

I have tried many things but all return errors - mostly "unknown modifier".

Mike
Re: not understanding pattern matching [message #178612 is a reply to message #178610] Wed, 11 July 2012 23:11 Go to previous messageGo to next message
Michael Joel is currently offline  Michael Joel
Messages: 42
Registered: October 2011
Karma: 0
Member
Michael Fesser wrote:

> .oO(Michael Joel)
>
>
>> I am wanting to change words in a string that has special tags.
>> Ex. "This is a %/i%special%i% word in the string."
>> If I want to replace any word between the %/i% and %i% what would I do?
>>
>> I messed with preg_repace but I couldn't understand the pattern system.
>
>
> What have you tried so far? Do you have any experiences with regular
> expressions?
>
> Micha
>

answer to your last question - No
Re: not understanding pattern matching [message #178613 is a reply to message #178611] Wed, 11 July 2012 23:26 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(Michael Joel)

> Michael Fesser wrote:
>
>> .oO(Michael Joel)
>>
>>
>>> I am wanting to change words in a string that has special tags.
>>> Ex. "This is a %/i%special%i% word in the string."
>>> If I want to replace any word between the %/i% and %i% what would I do?
>>>
>>> I messed with preg_repace but I couldn't understand the pattern system.
>>
>>
>> What have you tried so far? Do you have any experiences with regular
>> expressions?
>>
>> Micha
>>
>
> I have tried many things but all return errors - mostly "unknown modifier".

Which means syntax error.

Please post your code.

Micha

--
http://mfesser.de/
Fotos | Blog | Flohmarkt
Re: not understanding pattern matching [message #178614 is a reply to message #178613] Wed, 11 July 2012 23:31 Go to previous messageGo to next message
Michael Joel is currently offline  Michael Joel
Messages: 42
Registered: October 2011
Karma: 0
Member
Michael Fesser wrote:

> .oO(Michael Joel)
>
>
>> Michael Fesser wrote:
>>
>>
>>> .oO(Michael Joel)
>>>
>>>
>>>
>>>> I am wanting to change words in a string that has special tags.
>>>> Ex. "This is a %/i%special%i% word in the string."
>>>> If I want to replace any word between the %/i% and %i% what would I do?
>>>>
>>>> I messed with preg_repace but I couldn't understand the pattern system.
>>>
>>>
>>> What have you tried so far? Do you have any experiences with regular
>>> expressions?
>>>
>>> Micha
>>>
>>
>> I have tried many things but all return errors - mostly "unknown modifier".
>
>
> Which means syntax error.
>
> Please post your code.
>
> Micha
>
I was trying to just first get it to replace the exact thing typed. Then
move on to replacing any word within the %/i% and %i%

$Scripture="this is a %/i%test%i%";
preg_replace("%/i%test%i%","XXXXXXXX",$Scripture);

This errors with Unknown modifier t
I tried what I thought was escaping it by putting a \test but that just
messes up more. See I haven't a clue.

Mike
Re: not understanding pattern matching [message #178615 is a reply to message #178614] Thu, 12 July 2012 01:32 Go to previous messageGo to next message
BootNic is currently offline  BootNic
Messages: 10
Registered: November 2010
Karma: 0
Junior Member
In article <V5KdnVyG6ph9kWPSnZ2dnUVZ_uWdnZ2d(at)earthlink(dot)com>,
Michael Joel <null(at)void(dot)void> wrote:

> Michael Fesser wrote:
>>
>> .oO(Michael Joel)
>>>
>>> Michael Fesser wrote:
>>>>
>>>> .oO(Michael Joel)
>>>> >
>>>> > I am wanting to change words in a string that has special tags.
>>>> > Ex. "This is a %/i%special%i% word in the string."
>>>> > If I want to replace any word between the %/i% and %i% what would I do?
>>>> >
>>>> > I messed with preg_repace but I couldn't understand the pattern system.
>>>>
>>>> What have you tried so far? Do you have any experiences with regular
>>>> expressions?
>>>
>>> I have tried many things but all return errors - mostly "unknown modifier".
>>
>> Which means syntax error.
>>
>> Please post your code.
>
> I was trying to just first get it to replace the exact thing typed. Then
> move on to replacing any word within the %/i% and %i%
>
> $Scripture="this is a %/i%test%i%";
> preg_replace("%/i%test%i%","XXXXXXXX",$Scripture);

http://php.net/manual/en/regexp.reference.delimiters.php

preg_replace("#%/i%test%i%#","XXXXXXXX",$Scripture);

> This errors with Unknown modifier t
> I tried what I thought was escaping it by putting a \test but that just
> messes up more. See I haven't a clue.



--
BootNic Wed Jul 11, 2012 09:32 pm
When I was young, I was put in a school for retarded kids for two years before
they realized I actually had a hearing loss...and they called ME slow!
*Kathy Buckley*
Re: not understanding pattern matching [message #178616 is a reply to message #178609] Thu, 12 July 2012 03:27 Go to previous messageGo to next message
Michael Joel is currently offline  Michael Joel
Messages: 42
Registered: October 2011
Karma: 0
Member
Michael Joel wrote:
> I am wanting to change words in a string that has special tags.
> Ex. "This is a %/i%special%i% word in the string."
> If I want to replace any word between the %/i% and %i% what would I do?
>
> I messed with preg_repace but I couldn't understand the pattern system.
>
> Mike

Since this function seems to elude me I simply wrote a samll function to
do what I want. I simply pass it the two needles, haystack and what I
want to replace with.

Thanks
Mike
Re: not understanding pattern matching [message #178620 is a reply to message #178614] Fri, 13 July 2012 03:22 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Wed, 11 Jul 2012 19:31:12 -0400, Michael Joel wrote:

> I was trying to just first get it to replace the exact thing typed. Then
> move on to replacing any word within the %/i% and %i%
>
> $Scripture="this is a %/i%test%i%";
> preg_replace("%/i%test%i%","XXXXXXXX",$Scripture);
>
> This errors with Unknown modifier t I tried what I thought was escaping
> it by putting a \test but that just messes up more. See I haven't a
> clue.

http://php.net/manual/en/regexp.reference.delimiters.php

When you use a regex, the first character inside your quotes is taken as
the pattern delimiter.

In your case, this is a "%" character.

The format of a regex string is:

"delimiter pattern delimiter optional-modifiers"

Hence, "%/i%t" is the pattern "/i" with the modifier "t"

Try:

preg_replace("!%/i%test%i%!","XXXXXXXX",$Scripture);

This will use the "!" character as the pattern delimiter and should, in
this specific case, solve your problem. Generally it's a good idea to
pick a pattern delimiter that does not have special meaning to regex and
is not part of your data. This isn't always easy.

Rgds

Denis McMahon
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Check out www.phpscribble.com
Next Topic: Lost $_SESSION in Google Chrome
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 21 20:48:13 GMT 2024

Total time taken to generate the page: 0.02397 seconds