Garethp Posted February 24, 2010 Share Posted February 24, 2010 Ok, so I want to be able to replace all instances of "[--M[0-9]--]" into $M[0-9]. For example "This is [--M[4]--] something to be [--M[1]--] matched [--M[0]--]." would become "This is " . $M[4] . " something to be " . $M[1] . " matched " . $M[0] . "."; At the moment I have $NewName = preg_replace("~\[--(M\[[0-9]+\])--\]~", $\\1, $Rename); But I've also tried "~\[--M\[([0-9]+)\]--\]~", $M[\\1] and "~\[--M\[([0-9]+)\]--\]~", "$M[\\1]" Anyway, for my current code, I'm getting error Parse error: syntax error, unexpected T_NS_SEPARATOR, expecting T_VARIABLE or '$' in C:\Program Files\EasyPHP-5.3.1\www\Rename.php on line 18 Where line 18 is the one with the preg_replace. Has anyone got any solutions to what I'm after? Quote Link to comment https://forums.phpfreaks.com/topic/193221-turning-string-m1-into-variable-m1/ Share on other sites More sharing options...
JAY6390 Posted February 24, 2010 Share Posted February 24, 2010 $M = array(1,2,3,4,5,6,7,8,9,10); $text = 'This is [--M[4]--] something to be [--M[1]--] matched [--M[0]--].'; $regex = '/\[--(M\[\d+\])--\]/e'; $out = preg_replace($regex, '$\\1', $text); echo $out; Output: This is 5 something to be 2 matched 1. Quote Link to comment https://forums.phpfreaks.com/topic/193221-turning-string-m1-into-variable-m1/#findComment-1017453 Share on other sites More sharing options...
cags Posted February 24, 2010 Share Posted February 24, 2010 Is $M an independent array from which you wish to insert the 4th item from into the string? Quote Link to comment https://forums.phpfreaks.com/topic/193221-turning-string-m1-into-variable-m1/#findComment-1017508 Share on other sites More sharing options...
Garethp Posted March 5, 2010 Author Share Posted March 5, 2010 $M = array(1,2,3,4,5,6,7,8,9,10); $text = 'This is [--M[4]--] something to be [--M[1]--] matched [--M[0]--].'; $regex = '/\[--(M\[\d+\])--\]/e'; $out = preg_replace($regex, '$\\1', $text); echo $out; Output: This is 5 something to be 2 matched 1. Thanks, this is exactly what I needed. And yes Cags, it is. I have a whole bunch of cbr files (comic boks) that aren't named the way I want. So I made a script to rename a folder of them at a time to the kind of style that I wanted. This was just a way of making it easier to adjust the Search and Replace method Quote Link to comment https://forums.phpfreaks.com/topic/193221-turning-string-m1-into-variable-m1/#findComment-1022011 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.