PHPEdit.net Community
   
1 2 3 4
Tutorials Tips Pasties Code Snippets
 

Home > Code snippets > PHP > Strings > Remove Invalid XML Characters

Remove Invalid XML Characters

Created by ltp, last update on 26/02/2008 17:30

When you are writing out an XML document there are certain characters that are not allowed in the document. If these characters exist, you will be unable to parse the document as per the XML specification.What this function will do is take a string, remove the invalid characters and then return the string back to you to insert into an XML document.

  1. /**
  2. * Removes invalid XML
  3. *
  4. * @access public
  5. * @param string $value
  6. * @return string
  7. */
  8. function stripInvalidXml($value)
  9. {
  10. $ret = "";
  11. $current;
  12. if (empty($value))
  13. {
  14. return $ret;
  15. }
  16.  
  17. $length = strlen($value);
  18. for ($i=0; $i < $length; $i++)
  19. {
  20. $current = ord($value{$i});
  21. if (($current == 0x9) ||
  22. ($current == 0xA) ||
  23. ($current == 0xD) ||
  24. (($current >= 0x20) && ($current <= 0xD7FF)) ||
  25. (($current >= 0xE000) && ($current <= 0xFFFD)) ||
  26. (($current >= 0x10000) && ($current <= 0x10FFFF)))
  27. {
  28. $ret .= chr($current);
  29. }
  30. else
  31. {
  32. $ret .= " ";
  33. }
  34. }
  35. return $ret;
  36. }

Dependencies

No special requirements are needed by this snippet

By logging in you will be able to:

  • Recommand this page to someone else
  • Monitor changes on this item
  • Rate this item
  • Post comments
  • Download this item
Login now!

 
PHPEdit User Community, © 2008 WaterProof SARL
Powered by PHPEdit