Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.