Jump to content

preg_replace issue when replace content has a dollar sign


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!

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.