converting & to & for XML in PHP - php
I am building a XML RSS for my page. And running into this error:
error on line 39 at column 46: xmlParseEntityRef: no name
Apparently this is because I cant have & in XML... Which I do in my last field row...
What is the best way to clean all my $row['field']'s in PHP so that &'s turn into &
Use htmlspecialchars to encode just the HTML special characters &, <, >, " and optionally ' (see second parameter $quote_style).
It's called htmlentities() and html_entity_decode()
Really should look in the dom xml functions in php. Its a bit of work to figure out, but you avoid problems like this.
Convert Reserved XML characters to Entities
function xml_convert($str, $protect_all = FALSE)
{
$temp = '__TEMP_AMPERSANDS__';
// Replace entities to temporary markers so that
// ampersands won't get messed up
$str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
if ($protect_all === TRUE)
{
$str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
}
$str = str_replace(array("&","<",">","\"", "'", "-"),
array("&", "<", ">", """, "'", "-"),
$str);
// Decode the temp markers back to entities
$str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
if ($protect_all === TRUE)
{
$str = preg_replace("/$temp(\w+);/","&\\1;", $str);
}
return $str;
}
Use
html_entity_decode($row['field']);
This will take and revert back to the & from & also if you have &npsb; it will change that to a space.
http://us.php.net/html_entity_decode
Cheers
Related
PHP Convert Unicode to text
I am receiving from a form the following urlencoded string %F0%9D%90%B4%F0%9D%91%99%F0%9D%91%92%F0%9D%91%97%F0%9D%91%8E%F0%9D%91%9B%F0%9D%91%91%F0%9D%91%9F%F0%9D%91%8E If I decode it I get the following formatted text: ๐ด๐๐๐๐๐๐๐๐ Is there any way with PHP to get the plain "Alejandra" text from the encoded or decoded string? I have tried without success several ways to do it with mb_convert_encoding($string, "UTF-16",mb_detect_encoding($string)) iconv('utf-16', 'utf-8', rawurldecode($string) and any other solution I could in stackoverflow. Edit: I tried the proposed solution $strAscii = iconv('UTF-8','ASCII//TRANSLIT',$str); but it deletes the special characters such as รกรฉรญรณรบรฑรง which we need to stay. Expected result input: ๐ด๐๐๐๐๐๐๐๐ output: Alejandra input: รlejandra output: รlejandra Thank you in advance.
urldecode or rawurldecode is sufficient. $string = "%F0%9D%90%B4%F0%9D%91%99%F0%9D%91%92%F0%9D%91%97%F0%9D%91%8E%F0%9D%91%9B%F0%9D%91%91%F0%9D%91%9F%F0%9D%91%8E"; $str = urldecode($string); var_dump($str); //string(36) "๐ด๐๐๐๐๐๐๐๐" Demo: https://3v4l.org/OMQ35 A special debugger gives me: string(36) UTF-8mb4. This means that there are also UTF-8 characters in the string that require 4 bytes. The character A is the Unicode character โ๐ดโ (U+1D434). Note: If the special UTF-8 characters cause problems, you can try to display the strings as ASCII characters with iconv. $strAscii = iconv('UTF-8','ASCII//TRANSLIT',$str); //string(9) "Alejandra"
What you are getting is called a "psuedo-alphabet", you can see a list of them here: https://qaz.wtf/u/convert.cgi. The one that you appear to be getting can be seen here: https://en.wikipedia.org/wiki/Mathematical_Alphanumeric_Symbols Basically what you need to do is take the string, split it and use a lookup table to convert it back to regular characters. This implementation is terribly efficient but that's because I grabbed the alphabets from the above Wikipedia page and was too lazy to reorganise it. function math_symbols_to_plain_text($input, $alphabet) { $alphabets = [ ['a','๐','๐','๐','๐บ','๐ฎ','๐ข','๐','๐ถ','๐ช','๐','๐','๐','๐'], ['b','๐','๐','๐','๐ป','๐ฏ','๐ฃ','๐','๐ท','๐ซ','๐','๐','๐','๐'], ['c','๐','๐','๐','๐ผ','๐ฐ','๐ค','๐','๐ธ','๐ฌ','๐ ','๐','๐','๐'], ['d','๐','๐','๐ ','๐ฝ','๐ฑ','๐ฅ','๐','๐น','๐ญ','๐ก','๐','๐','๐'], ['e','๐','๐','๐','๐พ','๐ฒ','๐ฆ','๐','โฏ','๐ฎ','๐ข','๐','๐','๐'], ['f','๐','๐','๐','๐ฟ','๐ณ','๐ง','๐','๐ป','๐ฏ','๐ฃ','๐','๐','๐'], ['g','๐ ','๐','๐','๐','๐ด','๐จ','๐','โ','๐ฐ','๐ค','๐','๐','๐'], ['h','๐ก','โ','๐','๐','๐ต','๐ฉ','๐','๐ฝ','๐ฑ','๐ฅ','๐','๐','๐'], ['i','๐ข','๐','๐','๐','๐ถ','๐ช','๐','๐พ','๐ฒ','๐ฆ','๐','๐','๐'], ['j','๐ฃ','๐','๐','๐','๐ท','๐ซ','๐','๐ฟ','๐ณ','๐ง','๐','๐','๐'], ['k','๐ค','๐','๐','๐','๐ธ','๐ฌ','๐ ','๐','๐ด','๐จ','๐','๐','๐'], ['l','๐ฅ','๐','๐','๐ ','๐น','๐ญ','๐ก','๐','๐ต','๐ฉ','๐','๐','๐'], ['m','๐ฆ','๐','๐','๐','๐บ','๐ฎ','๐ข','๐','๐ถ','๐ช','๐','๐','๐'], ['n','๐ง','๐','๐','๐','๐ป','๐ฏ','๐ฃ','๐','๐ท','๐ซ','๐','๐','๐'], ['o','๐จ','๐','๐','๐','๐ผ','๐ฐ','๐ค','โด','๐ธ','๐ฌ','๐','๐','๐ '], ['p','๐ฉ','๐','๐','๐','๐ฝ','๐ฑ','๐ฅ','๐ ','๐น','๐ญ','๐','๐','๐ก'], ['q','๐ช','๐','๐','๐','๐พ','๐ฒ','๐ฆ','๐','๐บ','๐ฎ','๐','๐','๐ข'], ['r','๐ซ','๐','๐','๐','๐ฟ','๐ณ','๐ง','๐','๐ป','๐ฏ','๐','๐','๐ฃ'], ['s','๐ฌ','๐ ','๐','๐','๐','๐ด','๐จ','๐','๐ผ','๐ฐ','๐','๐','๐ค'], ['t','๐ญ','๐ก','๐','๐','๐','๐ต','๐ฉ','๐','๐ฝ','๐ฑ','๐','๐','๐ฅ'], ['u','๐ฎ','๐ข','๐','๐','๐','๐ถ','๐ช','๐','๐พ','๐ฒ','๐','๐','๐ฆ'], ['v','๐ฏ','๐ฃ','๐','๐','๐','๐ท','๐ซ','๐','๐ฟ','๐ณ','๐','๐','๐ง'], ['w','๐ฐ','๐ค','๐','๐','๐','๐ธ','๐ฌ','๐','๐','๐ด','๐','๐ ','๐จ'], ['x','๐ฑ','๐ฅ','๐','๐','๐ ','๐น','๐ญ','๐','๐','๐ต','๐','๐ก','๐ฉ'], ['y','๐ฒ','๐ฆ','๐','๐','๐','๐บ','๐ฎ','๐','๐','๐ถ','๐','๐ข','๐ช'], ['z','๐ณ','๐ง','๐','๐','๐','๐ป','๐ฏ','๐','๐','๐ท','๐','๐ฃ','๐ซ'], ['A','๐','๐ด','๐จ','๐ ','๐','๐','๐ผ','๐','๐','๐','๐ฌ','๐ฐ','๐ธ'], ['B','๐','๐ต','๐ฉ','๐ก','๐','๐','๐ฝ','โฌ','๐','๐ ','๐ญ','๐ฑ','๐น'], ['C','๐','๐ถ','๐ช','๐ข','๐','๐','๐พ','๐','๐','โญ','๐ฎ','๐ฒ','โ'], ['D','๐','๐ท','๐ซ','๐ฃ','๐','๐','๐ฟ','๐','๐','๐','๐ฏ','๐ณ','๐ป'], ['E','๐','๐ธ','๐ฌ','๐ค','๐','๐','๐','โฐ','๐','๐','๐ฐ','๐ด','๐ผ'], ['F','๐ ','๐น','๐ญ','๐ฅ','๐','๐','๐','โฑ','๐','๐','๐ฑ','๐ต','๐ฝ'], ['G','๐','๐บ','๐ฎ','๐ฆ','๐','๐','๐','๐ข','๐','๐','๐ฒ','๐ถ','๐พ'], ['H','๐','๐ป','๐ฏ','๐ง','๐','๐','๐','โ','๐','โ','๐ณ','๐ท','โ'], ['I','๐','๐ผ','๐ฐ','๐จ','๐','๐','๐','โ','๐','โ','๐ด','๐ธ','๐'], ['J','๐','๐ฝ','๐ฑ','๐ฉ','๐','๐','๐ ','๐ฅ','๐','๐','๐ต','๐น','๐'], ['K','๐','๐พ','๐ฒ','๐ช','๐','๐','๐','๐ฆ','๐','๐','๐ถ','๐บ','๐'], ['L','๐','๐ฟ','๐ณ','๐ซ','๐','๐','๐','โ','๐','๐','๐ท','๐ป','๐'], ['M','๐','๐','๐ด','๐ฌ','๐ ','๐','๐','โณ','๐','๐','๐ธ','๐ผ','๐'], ['N','๐','๐','๐ต','๐ญ','๐ก','๐','๐','๐ฉ','๐','๐','๐น','๐ฝ','โ'], ['O','๐','๐','๐ถ','๐ฎ','๐ข','๐','๐','๐ช','๐','๐','๐บ','๐พ','๐'], ['P','๐','๐','๐ท','๐ฏ','๐ฃ','๐','๐','๐ซ','๐','๐','๐ป','๐ฟ','โ'], ['Q','๐','๐','๐ธ','๐ฐ','๐ค','๐','๐','๐ฌ','๐ ','๐','๐ผ','๐','โ'], ['R','๐','๐ ','๐น','๐ฑ','๐ฅ','๐','๐','โ','๐ก','โ','๐ฝ','๐','โ'], ['S','๐','๐','๐บ','๐ฒ','๐ฆ','๐','๐','๐ฎ','๐ข','๐','๐พ','๐','๐'], ['T','๐','๐','๐ป','๐ณ','๐ง','๐','๐','๐ฏ','๐ฃ','๐','๐ฟ','๐','๐'], ['U','๐','๐','๐ผ','๐ด','๐จ','๐','๐','๐ฐ','๐ค','๐','๐','๐','๐'], ['V','๐','๐','๐ฝ','๐ต','๐ฉ','๐','๐','๐ฑ','๐ฅ','๐','๐','๐ ','๐'], ['W','๐','๐','๐พ','๐ถ','๐ช','๐','๐','๐ฒ','๐ฆ','๐','๐','๐','๐'], ['X','๐','๐','๐ฟ','๐ท','๐ซ','๐','๐','๐ณ','๐ง','๐','๐','๐','๐'], ['Y','๐','๐','๐','๐ธ','๐ฌ','๐ ','๐','๐ด','๐จ','๐','๐','๐','๐'], ['Z','๐','๐','๐','๐น','๐ญ','๐ก','๐','๐ต','๐ฉ','โจ','๐ ','๐','โค'] ]; $replace = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; $lookup = [ 'serif-normal', 'serif-bold', 'serif-italic', 'serif-bolditalic', 'sans-normal', 'sans-bold', 'sans-italic', 'sans-bolditalic', 'script-normal', 'script-bold', 'franktur-normal', 'fraktur-bold', 'monospace', 'doublestruck' ]; $map_index = array_search($alphabet, $lookup); $split = mb_str_split($input); $output = ''; foreach ($split as $char) { foreach ($alphabets as $i => $letter) { if ($letter[$map_index] === $char) $output .= $replace[$i]; } } return $output; } $input = '๐ด๐๐๐๐๐๐๐๐'; $output = math_symbols_to_plain_text($input, 'serif-italic'); echo $input . PHP_EOL . $output . PHP_EOL; Yields: ๐ด๐๐๐๐๐๐๐๐ Alejandra
If I am not wrong, you are trying to decode URL then why you are not trying to use urldecode() follow this .PHP DOC
PHP simplexml_load_file and LIBXML_NOENT [duplicate]
I have a php file which prints an xml based on a MySql db. I get an error every time at exactly the point where there is an & sign. Here is some php: $query = mysql_query($sql); $_xmlrows = ''; while ($row = mysql_fetch_array($query)) { $_xmlrows .= xmlrowtemplate($row); } function xmlrowtemplate($dbrow){ return "<AD> <CATEGORY>".$dbrow['category']."</CATEGORY> </AD> } The output is what I want, i.e. the file outputs the correct category, but still gives an error. The error says: xmlParseEntityRef: no name And then it points to the exact character which is a & sign. This complains only if the $dbrow['category'] is something with an & sign in it, for example: "cars & trucks", or "computers & telephones". Anybody know what the problem is? BTW: I have the encoding set to UTF-8 in all documents, as well as the xml output.
& in XML starts an entity. As you haven't defined an entity &WhateverIsAfterThat an error is thrown. You should escape it with &. $string = str_replace('&', '&', $string); How do I escape ampersands in XML To escape the other reserved characters: function xmlEscape($string) { return str_replace(array('&', '<', '>', '\'', '"'), array('&', '<', '>', ''', '"'), $string); }
$string =htmlspecialchars($string,ENT_XML1); is the most universal way to solve all encoding errors (IMHO better that write custom functions + there is no point to solve just &). Credit: Put Wrikken's and joshweir's comment as answer to be more visible.
You need to either turn & into its entity &, or wrap the contents in CDATA tags. If you choose the entity route, there are additional characters you need to turn into entities: > > < < ' ' " " Background: Beware of the ampersand when using XML Wikipedia: List of XML character entity references
Switch and regex with using xml escape function. function XmlEscape(str) { if (!str || str.constructor !== String) { return ""; } return str.replace(/[\"&><]/g, function (match) { switch (match) { case "\"": return """; case "&": return "&"; case "<": return "<"; case ">": return ">"; } }); };
public function sanitize(string $data) { return str_replace('&', '&', $data); } You are right: here is more context - the example is in relation to the ' how to deal with data containing '&' when we pass this data to SimpleXml. Of course there is also other solution to use <![CDATA[some stuff]]>
Replace characters with word in PHP?
Want to replace specific letters in a string to a full word. I'm using: function spec2hex($instr) { for ($i=0; $i<strlen($instr); $i++) { $char = substr($instr, $i,1); if ($char == "a"){ $char = "hello"; } $convString .= "&#".ord($char).";"; } return $convString; } $myString = "adam"; $convertedString = spec2hex($myString); echo $convertedString; but that's returning: hdhm How do I do this? By the way, this is to replace punctuation with hex characters. Thanks all.
Use http://php.net/substr_replace substr_replace($instr, $word, $i,1);
ord() expects only a SINGLE character. You're passing in hello, so ord is doing its thing only on the h: php > echo ord('hello'); 104 php > echo ord('h'); 104 So in effect your output is actually hdhm
it you want to use your same code just change $convString .= "&#".ord($char).";"; to $convString .= $char;
If you just want to replace the occurrence of a with hello within the string you pass to the function, why not use PHP's str_replace()? function spec2hex($instr) { return str_replace("a","hello",$instr); }
I must assume that you don't want to have hex characters instead of punctuation but html entities. Be aware that str_replace(), when called with arrays, will run over the string for multiple times, thus replacing the ";" in "{" also! Your posted code is not useful for replacing punctuation. use strtr() with arrays, it doesn't have the drawback of str_replace(). $aReplacements = array(',' => ',', '.' => '.'); //todo: complete the array $sText = strtr($sText, $aReplacements);
XML error at ampersand (&)
I have a php file which prints an xml based on a MySql db. I get an error every time at exactly the point where there is an & sign. Here is some php: $query = mysql_query($sql); $_xmlrows = ''; while ($row = mysql_fetch_array($query)) { $_xmlrows .= xmlrowtemplate($row); } function xmlrowtemplate($dbrow){ return "<AD> <CATEGORY>".$dbrow['category']."</CATEGORY> </AD> } The output is what I want, i.e. the file outputs the correct category, but still gives an error. The error says: xmlParseEntityRef: no name And then it points to the exact character which is a & sign. This complains only if the $dbrow['category'] is something with an & sign in it, for example: "cars & trucks", or "computers & telephones". Anybody know what the problem is? BTW: I have the encoding set to UTF-8 in all documents, as well as the xml output.
& in XML starts an entity. As you haven't defined an entity &WhateverIsAfterThat an error is thrown. You should escape it with &. $string = str_replace('&', '&', $string); How do I escape ampersands in XML To escape the other reserved characters: function xmlEscape($string) { return str_replace(array('&', '<', '>', '\'', '"'), array('&', '<', '>', ''', '"'), $string); }
$string =htmlspecialchars($string,ENT_XML1); is the most universal way to solve all encoding errors (IMHO better that write custom functions + there is no point to solve just &). Credit: Put Wrikken's and joshweir's comment as answer to be more visible.
You need to either turn & into its entity &, or wrap the contents in CDATA tags. If you choose the entity route, there are additional characters you need to turn into entities: > > < < ' ' " " Background: Beware of the ampersand when using XML Wikipedia: List of XML character entity references
Switch and regex with using xml escape function. function XmlEscape(str) { if (!str || str.constructor !== String) { return ""; } return str.replace(/[\"&><]/g, function (match) { switch (match) { case "\"": return """; case "&": return "&"; case "<": return "<"; case ">": return ">"; } }); };
public function sanitize(string $data) { return str_replace('&', '&', $data); } You are right: here is more context - the example is in relation to the ' how to deal with data containing '&' when we pass this data to SimpleXml. Of course there is also other solution to use <![CDATA[some stuff]]>
Revert escaped characters
I saved some data in the database using mysql_real_escape_string() so the single quotes are escaped like this '. It looks ok in the browser, but how can I convert it back to single quote when I save the text in a txt file?
Please note that mysql_real_escape_string() does not turn apostrophes ' into ' Only HTML-oriented functions do, so you must have calls to htmlentities() somewhere in your script. As for your question, the function you're looking for is html_entity_decode() echo html_entity_decode(''', ENT_QUOTES);
This is the reason why you should not store encoded text in the database. You should have stored it in it's original format, and encoded it when you display it. Now you have to check what characters the function does encode, and write string replacements that converts them back, in reverse order. Pseudo-code example: s = Replace(s, "'", "'") s = Replace(s, "<", "<") s = Replace(s, ">", ">") s = Replace(s, "&", "&")
That is just an ascii value of "'", use chr to get it back to a character. Here's the code $string = "Hello ' Man"; $string = preg_replace('|&#(\d{1,3});|e', 'chr(\1)', $string); echo $string; # Hello ' Man