Advertisement
murp

Remove Bad Characters that break json

Feb 6th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. <cfcomponent output="no">
  2. <cffunction name="XMLHighSafe" access="public" returntype="string" output="no" hint="This scans through a string, finds any characters that have a higher ASCII numeric value greater than 127 and automatically convert them to a hexadecimal numeric character">
  3. <cfargument name="text" type="string" required="yes">
  4. <cfscript>
  5. var i = 0;
  6. var tmp = '';
  7. while(ReFind('[^\x00-\x7F]',text,i,false))
  8. {
  9. i = ReFind('[^\x00-\x7F]',text,i,false); // discover high chr and save it's numeric string position.
  10. tmp = '&##x#FormatBaseN(Asc(Mid(text,i,1)),16)#;'; // obtain the high chr and convert it to a hex numeric chr.
  11. text = Insert(tmp,text,i); // insert the new hex numeric chr into the string.
  12. text = RemoveChars(text,i,1); // delete the redundant high chr from string.
  13. i = i+Len(tmp); // adjust the loop scan for the new chr placement, then continue the loop.
  14. }
  15. return text;
  16. </cfscript>
  17. </cffunction>
  18. </cfcomponent>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement