bcamp1973 Posted February 22, 2008 Share Posted February 22, 2008 i want to parse the page content looking for one or more instances of the string %MATCH-XX% where XX is an integer. I then want to replace that string with the code generated from a function that uses the XX integer as it's only argument. So, for example... i start with $content = '<p>This paragraph has a %MATCH-23% in it</p>'; and then my function function parse_match($XX,$content){ // work some magic with $content & $XX return '<span>with a number'.$XX.'</span>'; } produces this <p>This paragraph has a <span>with a number 23</span> in it</p> I need to be able to do this in PHP 4.1 as well...i'm at a loss at this point Link to comment https://forums.phpfreaks.com/topic/92485-string-replace-with-function/ Share on other sites More sharing options...
Bauer418 Posted February 22, 2008 Share Posted February 22, 2008 <?php preg_replace("/\%MATCH-(\d+)\%/ie", "parse_match('$1')", $content); ?> Link to comment https://forums.phpfreaks.com/topic/92485-string-replace-with-function/#findComment-473821 Share on other sites More sharing options...
bcamp1973 Posted February 22, 2008 Author Share Posted February 22, 2008 awesome! that works for one instance of %MATCH-XX%. how do i deal with multiple instances? Link to comment https://forums.phpfreaks.com/topic/92485-string-replace-with-function/#findComment-473830 Share on other sites More sharing options...
jeremyphphaven Posted February 22, 2008 Share Posted February 22, 2008 Are we sure this will work with the version of you are using? I'm have mad problems with regex on older PHP versions. There is a great changelog for the functionality on the PHP.net site, but it's hard to find, and I didn't bookmark it. So search around on the php.net site for the changelog if that regex code doesn't work on your version of php, they usually have suggestions Link to comment https://forums.phpfreaks.com/topic/92485-string-replace-with-function/#findComment-473832 Share on other sites More sharing options...
Bauer418 Posted February 22, 2008 Share Posted February 22, 2008 Try this out: <?php function parse_match($matches) { return '<span>with a number ' . $matches[0] . '</span>'; } $content = 'This is a string %MATCH-23% in it'; $content = preg_replace_callback('/\%MATCH-(\d+)\%/i', 'parse_match', $content); print $content; ?> Link to comment https://forums.phpfreaks.com/topic/92485-string-replace-with-function/#findComment-473839 Share on other sites More sharing options...
bcamp1973 Posted February 23, 2008 Author Share Posted February 23, 2008 Try this out: <?php function parse_match($matches) { return '<span>with a number ' . $matches[0] . '</span>'; } $content = 'This is a string %MATCH-23% in it'; $content = preg_replace_callback('/\%MATCH-(\d+)\%/i', 'parse_match', $content); print $content; ?> that did it! thanks Bauer418! Link to comment https://forums.phpfreaks.com/topic/92485-string-replace-with-function/#findComment-474563 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.