0

I am getting data from a json array which contains the special character "Â " which I'd like to remove.

$jsonString = preg_replace("#Â+\s#", "", $jsonString);
$jsonString = preg_replace("#<p>Â.*</p>#", "", $jsonString);
$jsonString = str_replace("Â&nbsp;", "", $jsonString);

The above lines are some of the code I've tried using to no avail. So the first question in mind is, is it even possible to use str_replace or preg_replace in json data? If not, is there any other function I can use to remove the specified special character from the string?

5
  • 1
    First off, is that your whole line? You need to pass the output back to the string: $jsonString=str_replace("Â&nbsp;", "", $jsonString); . Also the first regex is invalid FYI - no delims. Commented Oct 9, 2013 at 13:59
  • Actually the line I have looks more like the one you just showed, I'll update my post. Thanks for the correction about my regex. I've updated the first one and gave it a try but the special character's still there. Commented Oct 9, 2013 at 14:00
  • You might need to look at the custom function mb_str_replace on PHP.net Commented Oct 9, 2013 at 14:03
  • 1
    You're taking the wrong approach. Ask yourself why you're getting the unwanted characters, rather than simply how to remove them once they're there. Don't try to hack your way around a problem like this; you have to deal with the root cause or you'll end up just causing further problems. The root problem here is your character encoding: fix that and the rogue characters will disappear without you having to hack anything. Commented Oct 9, 2013 at 14:13
  • Yup, that's exactly what my problem was. Thanks for the advice. Commented Oct 9, 2013 at 14:15

1 Answer 1

3

JSON is always encoded in UTF-8. If your PHP file is not, then it's looking for the wrong thing.

Try this:

$jsonString = str_replace(utf8_encode("Â"),"",$jsonString);

That being said, are you sure you want to do this? I'm pretty sure you're working with a © symbol, and because of the UTF-8 conversion the result is © - surprise, exactly what you're asking to have removed.

Make sure your encoding is correct. Use utf8_decode if you need to.

Sign up to request clarification or add additional context in comments.

3 Comments

I just thought about the possibility that it had something to do with the json's encoding so I was looking it up when you posted this. Solved my problem. Thanks a lot.
About your question, I'm not entirely sure if it's really a © symbol because I've encountered this problem before with a normal string which I solved using the regular expression I included in my original post. It was actually added right next to a space which if you view the source would be &nbsp;
@user1597438 - The &nbsp; entity code converts into UTF-8 character code 160; it shows up as a blank space, but is a different character to a normal space character. Since this is an extended character outside the base ASCII range, it will have the same issue as the copyright symbol or any other extended character if your encoding is wrong.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.