AviNahum Posted July 7, 2009 Share Posted July 7, 2009 hey, i'm trying to convert some special combination to images... it's my first time i'm try to use regular expressions... this is the function i using: public function convert($txt) { global $DB; $txt = preg_replace( "<{img_(.+?)\}>", "<img src='\\1\.gif'>", $txt ); return $txt; } in simple words, i want it to convert the combination <{img_1}> to <img src='1.gif'> the number 1 (<{img_1}> ) can change to any number... this function wont work and shows me an error: Warning: preg_replace() [function.preg-replace]: Unknown modifier 'g' in C:\wamp\www\cms\functions.php on line 260 Thanks! ***sorry for poor english*** Quote Link to comment https://forums.phpfreaks.com/topic/165087-solved-regular-expressions-problem/ Share on other sites More sharing options...
Maq Posted July 7, 2009 Share Posted July 7, 2009 You're missing delimiters. This tutorial covers basic regex (I think delimiters are on page 3) - http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax/page1. Quote Link to comment https://forums.phpfreaks.com/topic/165087-solved-regular-expressions-problem/#findComment-870528 Share on other sites More sharing options...
AviNahum Posted July 7, 2009 Author Share Posted July 7, 2009 thanks for trying to help, but i already tried this tutorial and i cant make it to work Quote Link to comment https://forums.phpfreaks.com/topic/165087-solved-regular-expressions-problem/#findComment-870530 Share on other sites More sharing options...
Maq Posted July 7, 2009 Share Posted July 7, 2009 thanks for trying to help, but i already tried this tutorial and i cant make it to work If you already tried that tutorial then you would know that you need to implement delimiters. Please read through it again or Google, I have already told you what the cause of the error is. Quote Link to comment https://forums.phpfreaks.com/topic/165087-solved-regular-expressions-problem/#findComment-870537 Share on other sites More sharing options...
AviNahum Posted July 7, 2009 Author Share Posted July 7, 2009 OK, thanks! i solved it! Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/165087-solved-regular-expressions-problem/#findComment-870539 Share on other sites More sharing options...
Maq Posted July 7, 2009 Share Posted July 7, 2009 OK, thanks! i solved it! Thanks again! Great, you were having the same problem as before with the delimiters because preg_match takes a regular expression. Please mark as solved. Quote Link to comment https://forums.phpfreaks.com/topic/165087-solved-regular-expressions-problem/#findComment-870541 Share on other sites More sharing options...
AviNahum Posted July 7, 2009 Author Share Posted July 7, 2009 i need some more help and i thought it's will be better if i post it here and dont open a new topic... $img_id = preg_replace( "/<{img_(.+?)\}>/", "\\1", $txt ); this code works fine, for example: this: <p> sad <{img_2}> asd </p> will be replaced with: <p> sad 2 asd </p> i want to remove all characters thats comes before and after the number "2" i tried this code: $img_id = preg_replace( "/(.+?)\/ /<{img_(.+?)\ /(.+?)\/}>/", "\\2", $txt ); but its wont work and shows this error: Warning: preg_replace() [function.preg-replace]: Unknown modifier '&' in C:\wamp\www\cms\functions.php on line 265 i tried a few another ways but still it's wont work... thanks! Quote Link to comment https://forums.phpfreaks.com/topic/165087-solved-regular-expressions-problem/#findComment-870556 Share on other sites More sharing options...
nrg_alpha Posted July 7, 2009 Share Posted July 7, 2009 Hmm.. Im not sure if I follow correctly.. do you mean something like this? $txt = <<<EOD <p> sad <{img_2}> asd </p> EOD; $img_id = preg_replace('#<{img_([^}]+)}>#', '\1', $txt ); echo $img_id; // output: sad 2 asd For the capture, I use ([^}]+) instead of \d+, in case there can be anything as opposed to only digits. If it will in fact only be digits, you can use this instead: ([0-9]+) or (\d+) [\d assumes there is no locale / exponent issues]. Quote Link to comment https://forums.phpfreaks.com/topic/165087-solved-regular-expressions-problem/#findComment-870618 Share on other sites More sharing options...
AviNahum Posted July 7, 2009 Author Share Posted July 7, 2009 assume that the var $txt contain this: <p> sad <{img_2}> asd </p> i want the output will be 2 i want all the content that comes before and after the number 2 will removed... and its important to me to replace the "<" and ">" with "<" and ">" i tried this: $img_id = preg_replace("/^(.+?)\<{img_(.+?)\}>(.+?)\$/", "\\2", $txt ); but its still wont work... and thanks for trying to help! Quote Link to comment https://forums.phpfreaks.com/topic/165087-solved-regular-expressions-problem/#findComment-870628 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.