PHP preg_replace

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a very specific need. Too long to get into what and why here (If
you please).

I have this test code...

$_str = "testing that'S supposed to work!";  // Notice the CAP 'S, this
is the key!

$suffixes = "'S";
$str = preg_replace("/(\w)($suffixes)\b/e", '"$1".strtolower("$2")',
echo $str;

It gives me this...


Notice the SLASH?

I don't want the slash. Beyond that, this does what I want it to do.

Please understand, this code is for example only. The question focuses
around the preg_replace(), not on how I get/create "$_str"

Any ideas?


Re: PHP preg_replace wrote:
Quoted text here. Click to load it

Unclear to me what purpose that statement serves.  Ie. does it benefit
you to state it?

Quoted text here. Click to load it

Seems interesting to me, too.  Evidently, php assumes that the inner
strings will be single quote delimited and pre escapes the match
strings.  To fix, swap the single and double quotes:
'"$1".strtolower("$2")'  =>  "'$1'.strtolower('$2')"

Csaba Gabor from Vienna

Site Timeline