pureDesi Posted May 2, 2007 Share Posted May 2, 2007 I'm trying to replace all occurrences of {image:*} with <img src='*' /> using eregi_replace and I'm getting some awkward results. The following code is what I used, the output is shown below. code in question: $string = "This is a test {image:5.bmp} why won't you work! {image:test.jpg} grr"; $pattern = "{image:(.+)}"; echo eregi_replace($pattern, "<img src='files/\\1' />", $string); output: This is a test <img src='files/5.bmp} why won't you work! {image:test.jpg' /> grr However, when there's only one occurrence of {img src='*'} in the string, it works like a charm. Do you guys have any clue as to why this is happening? Thanks Quote Link to comment Share on other sites More sharing options...
corbin Posted May 2, 2007 Share Posted May 2, 2007 The pattern is wrong.... Technically that matches correctly since . can be any character and theres a /> farther down the road.... I would tell you the correct pattern, but I fail miserably at regexp and would probably make things worse. ;p Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted May 2, 2007 Share Posted May 2, 2007 His pattern's fine.. he's just missing an option Use the "U" option to "ungreedy" the pattern. <?php $string = "This is a test {image:5.bmp} why won't you work! {image:test.jpg} grr"; echo preg_replace("/{image:(.+)}/U", "<img src='files/\\1' />", $string); ?> Quote Link to comment Share on other sites More sharing options...
corbin Posted May 2, 2007 Share Posted May 2, 2007 Ooohhh.... Hehe I fail at regexp ;p Quote Link to comment Share on other sites More sharing options...
pureDesi Posted May 2, 2007 Author Share Posted May 2, 2007 I appreciate the help, I got it working using generic's code. Cheers. Quote Link to comment Share on other sites More sharing options...
pureDesi Posted May 2, 2007 Author Share Posted May 2, 2007 Crap - ran into a problem, how would you do case insensitive using preg_replace ? EDIT: I take that back! Just looked it up on php.net, modifiers FTW! Thanks again. Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted May 3, 2007 Share Posted May 3, 2007 not sure... but maybe you can add A-Z somewhere in the pattern? like this maybe? (just a thought, please correct me if i'm wrong) preg_replace("/{[A-Z]}{image:(.+)}/U", "<img src='files/\\1' />", $string); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.