0

I want to change strings with the odd """ such as:

He said: "I don't think so"

To be:

He said: "I don't think so"

My current code is:

$sentence = addslashes(preg_replace('/^\&quot\;$/','\"',$var));

What is my problem in the code?

0

3 Answers 3

1

^ and $ will only match the start and end of the whole string (or a whole line in /m mode). Since " doesn't appear like that, your regex whole match it. Just remove the ^ and $ and it should work.

BTW, perhaps you want to use html_entity_decode() instead.

0
1

You are probably better off using PHPs htmlspecialchars_decode():

$var = "He said: "I don't think so"";
$sentence = htmlspecialchars_decode($var);
1

This one may solve your problem:

$yourstring = "He said: "I don't think so"";
$newstring = str_replace(""","\"",$yourstring);

echo $newstring;
1
  • +1 for noting that the target is a fixed length literal string. For this case str_replace() will be faster than preg_replace(). Dec 26, 2011 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.