mrberti Posted March 11, 2007 Share Posted March 11, 2007 Hello, I'm attempting to pass a user-defined function in an ereg_replace, but having difficulties. $text = "blah 9 blah"; echo eregi_replace("([1-9])", doSomething('\\1'), $text); function doSomething($i) { echo $i."<br />"; if($i == "9") return "foo"; else return "~$i~"; } outputs... \1 blah ~9~ blah So in doSomething(), $i goes through correctly on the RETURN, but not on the ECHO or IF... I guess I am handling $i incorrectly? Any help is appreciated. Link to comment https://forums.phpfreaks.com/topic/42194-ereg_replace-function/ Share on other sites More sharing options...
Glyde Posted March 11, 2007 Share Posted March 11, 2007 Try a preg_replace: $text = "blah 9 blah"; echo preg_replace("/([1-9])/se", "doSomething('\\1')", $text); function doSomething($i) { echo $i . "<br />"; if ($i == "9") return "foo"; else return "~$i~"; } Link to comment https://forums.phpfreaks.com/topic/42194-ereg_replace-function/#findComment-204826 Share on other sites More sharing options...
mrberti Posted March 11, 2007 Author Share Posted March 11, 2007 Thanks! I took your code and applied to perfectly to my script, but I encountered another problem. How do you do an 'except' clause in preg? For example, I would like to do "match any single character (letter, number, special character, or otherwise.. anything) except Z"? Link to comment https://forums.phpfreaks.com/topic/42194-ereg_replace-function/#findComment-204888 Share on other sites More sharing options...
Glyde Posted March 11, 2007 Share Posted March 11, 2007 Add a ^ to a character set. Example regex: [^zZ] Would match any character except a lowercase or capital Z Link to comment https://forums.phpfreaks.com/topic/42194-ereg_replace-function/#findComment-204994 Share on other sites More sharing options...
mrberti Posted March 15, 2007 Author Share Posted March 15, 2007 OK... I'm trying to find and replace links in text. for example... The [[G||Chrono Trigger]] soundtrack proved extremely popular with fans. Mitsuda worked on four more titles for Squaresoft, the last being [[G||Xenogears]] in 1998. I would like to find, analyze, and replace [[G||...]] with a link. So how would you do a preg match? So far I have: /\[\[G\|\|([- a-z0-9]+)\]\]/ise but I want to make an expression that says "match ANY character within the brackets" (including special and foreign characters that may be there). Thanks again. Link to comment https://forums.phpfreaks.com/topic/42194-ereg_replace-function/#findComment-207661 Share on other sites More sharing options...
effigy Posted March 15, 2007 Share Posted March 15, 2007 <pre> <?php $string = 'The [[G||Chrono Trigger]] soundtrack proved extremely popular with fans. Mitsuda worked on four more titles for Squaresoft, the last being [[G||Xenogears]] in 1998.'; preg_match_all('/(?<=\[\[G\|\|)([^]]+)(?=\]\])/', $string, $matches); print_r($matches); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/42194-ereg_replace-function/#findComment-207679 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.