Skip to content

Instantly share code, notes, and snippets.

@pifantastic
Forked from elliotttf/xml.php
Created April 26, 2011 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pifantastic/943220 to your computer and use it in GitHub Desktop.
Save pifantastic/943220 to your computer and use it in GitHub Desktop.
<?php
/**
* Escape special characters.
*
* See http://en.wikipedia.org/wiki/XML#Escaping
*/
function ec_dreammail_escape_xml($string) {
$search = array('<', '>', '&', '"', "'");
$replace = array('&lt;', '&gt;', '&amp;', '&quot;', '&apos;');
return str_replace($search, $replace, $string);
}
/**
* Returns FALSE if there are unsafe characters.
*/
function ec_dreammail_sanitize_xml($string) {
$length = strlen($string);
for ($i = 0; $i < $length; $i++) {
$current = ord($string[$i]);
if (!(($current == 0x9) || ($current == 0xA) || ($current == 0xD) ||
(($current >= 0x20) && ($current <= 0xD7FF)) ||
(($current >= 0xE000) && ($current <= 0xFFFD)) ||
(($current >= 0x10000) && ($current <= 0x10FFFF)))) {
return FALSE;
}
}
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment