Regex replacement issue

Hi all,

I'm having a bit of trouble with regex replacement. I'm trying to convert HTML image tags into CakePHP image code. I can't seem to get it to work.

This is my search string:

<img src="images/([^"]*)" width="([0-9]*)" height="([0-9]*)" alt="" />

I'm trying to use this as a replace string:

<\?php echo \$html\->image\('$1', array\('width' => $2, 'height' => $3, 'alt' => ''\)\); \?>

This doesn't work, as I get a 'malformed replacement string' error. I've tried several variations to the above and found out that this one does work:

<\?php echo image\('$1', array\('width' => $2, 'height' => $3, 'alt' => ''\)\); \?>

But, as you can see I had to drop the $html-> part. What am I missing here? It's driving me nuts. Tried various escaping methods, but all return the malformed string error.

1 comment
Comment actions Permalink

Try this. I used the code to insert the unicode character for dollar sign. Seems to work.


<\?php echo \\\u0024html\->image\('$1', array\('width' => $2, 'height' => $3, 'alt' => ''\)\); \?>

Got the idea from seeing that you have to double escape backslashes on this site.


Please sign in to leave a comment.