Jump to content

Turning String "[--M[1]--]" into variable $M[1]


Garethp

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/193221-turning-string-m1-into-variable-m1/
Share on other sites

$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.

  • 2 weeks later...

$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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.