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. Quote Link to comment 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~"; } Quote Link to comment 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"? Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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> 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.