Jul
7
2010
7
2010
Convert all applicable characters to Numeric entities for use in XML
If you wanna make sure your text gets parsed correctly you mostly use htmlentities. However this method has 2 downsides:
1. It does not convert in to numeric entities so you’ll have problems when parsing as XML
2. It does NOT cover all characters that are like to show up.
So, to address this Issues, first for Point 1:
function _convertAlphaEntitysToNumericEntitys($entity){
return '&#'.ord(html_entity_decode($entity[0])).';';
}
$content = preg_replace_callback('/&([\w\d]+);/i','_convertAlphaEntitysToNumericEntitys',$content);
Here all “normal” entities are taken (which you already have, using htmlentities) and replaced by their numeric counterparts so they can be parsed as XML, now that leaves us with our second Problem, the Fact that only a small range of characters is covered in the first Place:
function _convertAsciOver127toNumericEntitys($entity){
if(($asciCode = ord($entity[0])) > 127){
return '&#'.$asciCode.';';
}else{
return $entity[0];
}
}
$content = preg_replace_callback('/[^\w\d ]/i','_convertAsciOver127toNumericEntitys'), $content);
And there you go, the resulting Text should have no entitie Problems in XML.
Related Posts
Leave a comment
sideblog
- replace your plesk favicon (god damn it!)
- Convert all applicable characters to Numeric entities for use in XML
- Drupal Fatal error: Call to undefined function field_file_load() in /home/johannes/workspace/drupal/sites/all/modules/filefield/filefield_field.inc on line 97 with php 5.3
- smashing magazine forum closed - poor call
- disillusioning results for using telekoms developer garden ip location service
tags
2010 ai analytics api app attachment barcamp bash berlin boss compressor conference css drupal filter finder google helper howto image js manual menu minify node options pagepath php plugin regexp search studivz unconference user experience uxcamp view wordpress wp xml x of y yahoo yui zend zend framework Zend_Gdata
An article by Hannes

















![Super Mario Supermarket of the Day: Awesome stock boy is awesome.
[reddit.] Super Mario Supermarket of the Day: Awesome stock boy is awesome.
[reddit.]](http://c0382052.cdn.cloudfiles.rackspacecloud.com/dz3hwo_s.jpg?1281352245)