Scooby08 Posted November 15, 2011 Share Posted November 15, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/251201-preg_replace-issue-when-replace-content-has-a-dollar-sign/ Share on other sites More sharing options...
freelance84 Posted November 15, 2011 Share Posted November 15, 2011 $ is a special symbol so needs to be negated, http://www.regular-expressions.info/characters.html this site is pretty awesome Quote Link to comment https://forums.phpfreaks.com/topic/251201-preg_replace-issue-when-replace-content-has-a-dollar-sign/#findComment-1288401 Share on other sites More sharing options...
xyph Posted November 15, 2011 Share Posted November 15, 2011 This is generally known as escaping a character Quote Link to comment https://forums.phpfreaks.com/topic/251201-preg_replace-issue-when-replace-content-has-a-dollar-sign/#findComment-1288491 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.