Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg, ignoring chars!!

Status
Not open for further replies.

ThomasJSmart

Programmer
Sep 16, 2002
634
can someone tell me why preg replace ignores the first of the characters im looking for?


$string = 'this is {some} string of {text}';

$replace = preg_replace_callback('/{.*}/i', "Creplace", $replace);

the returned match[0] = '{some} string of {text}'

so whats wrong with the '}' after some?? why is that one being ignored. i should be getting '{some}' returned or am i missing something?

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
you may be misusing the function. because i'm not sure how you get $match[0] from your script.

can you post the whole code segment, including the callback?

 
oh i see. now that i've read the whole post....

this is a greediness issue. by nature the * is greedy and so will go to the last match that it can get.
just reduce the greediness by adding a question mark after the star.

Manual Ref
php manual said:
However, if a quantifier is followed by a question mark, then it ceases to be greedy, and instead matches the minimum number of times possible
 
greedy code?? haha that is kind of funny.,

thanks for ? tip, works like a charm :)

i did also notice however the a \n ends the greedyness.

so in the case of

$string = 'this is {some} string
of {text}';

it did actually work correctly. interesting ^^

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
that is to do with dot match all. if you want to go across line breaks add an 's' to the modifiers.
e.g.
Code:
$pattern = '/matches/i[red]s[/red]';
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top