Jump to content

Weird eregi_replace problem


pureDesi

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/49719-weird-eregi_replace-problem/
Share on other sites

His pattern's fine.. he's just missing an option :D

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);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.