1

I have an xml document that gets loaded onto a page. Sometimes there are specific characters that cannot be parsed and shows this symbol in place of what should be there: –

Sometimes the character varies from a hyphen, to an apostrophe, to even a double quote.

What I'd like to do is, create an array:

$invalidCharacters = array(" – ", "’", "&");

and if the string contains any of those characters, replace them with their HTML/ASCII equivalent. like this: " – ", "'", and &.

I know that I can do a str_replace() on some items, but, is there a simple way to have it go trough a loop and look for the specific characters, replacing each as it goes?

2
  • str_replace with arrays just performs all the replacements sequentially. Commented Apr 2, 2014 at 20:33
  • Getting – sounds more like a character set issue than anything, e.g. dumping utf8 text into an iso8859 environment. Commented Apr 2, 2014 at 20:40

1 Answer 1

2

Using htmlspecialchars should work for you.

http://docs.php.net/manual/en/function.htmlspecialchars.php

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

2 Comments

So, wait... all I would have to do is something like this? $MyString = htmlspecialchars($myotherstring); And that's it??
Yes, you could add a flag for XML. It would look like $MyString = htmlspecialchars($myotherstring, ENT_XML1);

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.