Jump to content

preg_replace issue when replace content has a dollar sign


Scooby08

Recommended Posts

Here's an example of my issue with the dollar sign..

 

This one treats the dollar sign like an expression..

<?php
$input = '<div>stuff</div>';
$replace = '<div>$600,000. Test? Test!</div>';
$output = preg_replace('/<div>(.*?)<\/div>/',$replace,$input);
echo $output; // <div>0,000. Test? Test!</div>
?>

 

So I tried out preg_quote and now the dollar sign is fixed, but the punctuation now has slashes which are not wanted..

<?php
$input = '<div>stuff</div>';
$replace = '<div>'.preg_quote('$600,000. Test? Test!').'</div>';
$output = preg_replace('/<div>(.*?)<\/div>/',$replace,$input);
echo $output; // <div>$600,000\. Test\? Test\!</div>
?>

 

I guess I'm looking for the best alternative to this situation so that dollar signs aren't treated like expressions and the punctuation does not have backslashes..

 

Thanks!

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.