Post by Jenna (CAN) on Jan 19, 2013 4:52:10 GMT
Hey everyone! Alright, users of PHP for sim-related websites, ever have issues storing, processing and/or retrieving strings that contain characters like é, ø, ÿ, ü, etc. ?
Do you hate the need to use stripslashes() and/or addslashes() ?
I fixed both with the same pair of functions!
Below are two PHP codes that I just wrote and tested on my own stable using up-to-date versions of Mozilla Firefox, Google Chrome, Safari and Internet Explorer.
They performed in exactly the same way in all four browsers (and in fact the second function is not needed unless your web host is running a version of HTML older than HTML4).
specialCharsInsert()
All of the characters listed in $asSeen are converted to their corresponding HTML ASCII codes. This makes all those non A-Z,a-z,0-9 characters database-friendly and easily interpreted by browsers. Since it is also encoding ` and ' characters, there is no need to add or remove slashes from your strings.
Note: I currently have ` being encoded as ', because most people use ` to avoid issues with ', but I think we all prefer ' if we can make it work. If you actually want your ` to remain as it is, replace the second instance of "'" in the $htmlSpecChars array with "`"
Yes, by the way, HTML codes including links do still work after " is encoded, as long as your web host's server is correctly interpreting the HTML ASCII codes.
If you find that the HTML ASCII codes are not being interpreted as desired, run your retrieved strings through the reverse function.
specialCharsDisplay()
This is literally the same thing as the first function with the order of the inputs of str_replace() re-arranged.
Again, if you want ` to appear as ` instead of ', replace the second "'" with "`"
***specialCharsDisplay() IS NOT NECESSARY if you are running HTML4 or higher***
Example of a page that is retrieving text and html codes containing HTML ASCII encoded strings that were encoded using specialCharsInsert(): GNs Debütantin
NOTE: If you have never visited my stable from your current internet network before, you will have to click through my disclaimer first. It will redirect you to the correct page when you click through.
The horse name in bold caps, top left above the photos, should read: GNS DEBÜTANTIN
If my web host were not interpreting the encoding correctly, you would see: GNS DEBÜTANTIN
(If you are seeing ü converted correctly but the name is not in all-caps then the PHP is still working correctly, but your browser does not support the CSS used to convert the header to all-caps.)
Do you hate the need to use stripslashes() and/or addslashes() ?
I fixed both with the same pair of functions!
Below are two PHP codes that I just wrote and tested on my own stable using up-to-date versions of Mozilla Firefox, Google Chrome, Safari and Internet Explorer.
They performed in exactly the same way in all four browsers (and in fact the second function is not needed unless your web host is running a version of HTML older than HTML4).
specialCharsInsert()
function specialCharsInsert($string){
$asSeen = array("'", "*", "@", "`", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", "®", "¯", "°", "±", "²", "³", "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ", "Œ", "œ", "Š", "š", "Ÿ", "ƒ", "†", "‡", "•", "…", "‰", "€", "™");
$htmlSpecChars = array("'", "*", "@", "'", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", "®", "¯", "°", "±", "²", "³", "'", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ", "Œ", "œ", "Š", "š", "Ÿ", "ƒ", "†", "‡", "•", "…", "‰", "€", "™");
$string = str_replace($asSeen, $htmlSpecChars, $string);
return $string;
}
All of the characters listed in $asSeen are converted to their corresponding HTML ASCII codes. This makes all those non A-Z,a-z,0-9 characters database-friendly and easily interpreted by browsers. Since it is also encoding ` and ' characters, there is no need to add or remove slashes from your strings.
Note: I currently have ` being encoded as ', because most people use ` to avoid issues with ', but I think we all prefer ' if we can make it work. If you actually want your ` to remain as it is, replace the second instance of "'" in the $htmlSpecChars array with "`"
Yes, by the way, HTML codes including links do still work after " is encoded, as long as your web host's server is correctly interpreting the HTML ASCII codes.
If you find that the HTML ASCII codes are not being interpreted as desired, run your retrieved strings through the reverse function.
specialCharsDisplay()
function specialCharsDisplay($string){
$asSeen = array("'", "*", "@", "`", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", "®", "¯", "°", "±", "²", "³", "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ", "Œ", "œ", "Š", "š", "Ÿ", "ƒ", "†", "‡", "•", "…", "‰", "€", "™");
$htmlSpecChars = array("'", "*", "@", "'", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", "®", "¯", "°", "±", "²", "³", "'", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ", "Œ", "œ", "Š", "š", "Ÿ", "ƒ", "†", "‡", "•", "…", "‰", "€", "™");
$string = str_replace($htmlSpecChars, $asSeen, $string);
return $string;
}
This is literally the same thing as the first function with the order of the inputs of str_replace() re-arranged.
Again, if you want ` to appear as ` instead of ', replace the second "'" with "`"
***specialCharsDisplay() IS NOT NECESSARY if you are running HTML4 or higher***
Example of a page that is retrieving text and html codes containing HTML ASCII encoded strings that were encoded using specialCharsInsert(): GNs Debütantin
NOTE: If you have never visited my stable from your current internet network before, you will have to click through my disclaimer first. It will redirect you to the correct page when you click through.
The horse name in bold caps, top left above the photos, should read: GNS DEBÜTANTIN
If my web host were not interpreting the encoding correctly, you would see: GNS DEBÜTANTIN
(If you are seeing ü converted correctly but the name is not in all-caps then the PHP is still working correctly, but your browser does not support the CSS used to convert the header to all-caps.)