Forum Moderators: coopster

Message Too Old, No Replies

Special character => Numerical code converter

         

Readie

11:07 pm on Jan 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I had need of a script to neaten up some names containing special characters being echoed from a MySQL database, and as I recall two people asking for help with this issue during my reletivley short time here, I've decided to make the scripting I used available here.

All symbols below are allowed in a file using ANSI encoding, and will work for a webpage using the utf-8 charsheet. If you want to have any more symbols being converted you will need to use a database to develop the arrays, as the rest of the symbols are not recognised when saving in ANSI format.

I wanted to actually upload the file, but a wall of text will have to suffice:

<?php

/*
PhP Special Character to Numerical Code converter
Compiled by Michael Read
Contact me by sending a PM to Readie at http:/www.webmasterworld.com
Code may be used freely for all purposes and without citation
*/

/*
SYNTAX1 (Suggested):
Have the following line above the (echo $variable) in your PhP document:

------
include 'symbolreplace.php';
------

Then you have your usual variable declaration of the SQL column, but with a preg_replace string:

------
$sql = "SELECT * FROM database_table WHERE 1";
$rows = mysql_num_rows($result);
$result = mysql_query($sql);
for ($i = 0; $i < $rows; $i++){
$name = preg_replace($symb, $repl, mysql_result($result,$i,"name"));
}
------

*/

/*
SYNTAX2 (Not suggested):
Have the following line above the (echo $variable) in your PhP document:

------
include 'symbolreplace.php';
------

Then you have your usual variable declaration of the SQL column:

------
$sql = "SELECT * FROM database_table WHERE 1";
$rows = mysql_num_rows($result);
$result = mysql_query($sql);
for ($i = 0; $i < $rows; $i++){
$name = mysql_result($result,$i,"name");
}
------

And then, in the PhP loop where you want the variable to appear you would have:

------
echo preg_replace($symb, $repl, $name);
------
*/

$symb = array();
$symb[0] = '/À/';
$symb[1] = '/à/';
$symb[2] = '/Á/';
$symb[3] = '/á/';
$symb[4] = '/Â/';
$symb[5] = '/â/';
$symb[6] = '/Ã/';
$symb[7] = '/ã/';
$symb[8] = '/Ä/';
$symb[9] = '/ä/';
$symb[10] = '/Å/';
$symb[11] = '/å/';
$symb[12] = '/Æ/';
$symb[13] = '/æ/';
$symb[14] = '/Ç/';
$symb[15] = '/ç/';
$symb[16] = '/Ð/';
$symb[17] = '/Ð/';
$symb[18] = '/ð/';
$symb[19] = '/È/';
$symb[20] = '/è/';
$symb[21] = '/É/';
$symb[22] = '/é/';
$symb[23] = '/Ê/';
$symb[24] = '/ê/';
$symb[25] = '/Ë/';
$symb[26] = '/ë/';
$symb[27] = '/ƒ/';
$symb[28] = '/Ì/';
$symb[29] = '/ì/';
$symb[30] = '/Í/';
$symb[31] = '/í/';
$symb[32] = '/Î/';
$symb[33] = '/î/';
$symb[34] = '/Ï/';
$symb[35] = '/ï/';
$symb[36] = '/Ñ/';
$symb[37] = '/ñ/';
$symb[38] = '/Ò/';
$symb[39] = '/ò/';
$symb[40] = '/Ó/';
$symb[41] = '/ó/';
$symb[42] = '/Ô/';
$symb[43] = '/ô/';
$symb[44] = '/Õ/';
$symb[45] = '/õ/';
$symb[46] = '/Ö/';
$symb[47] = '/ö/';
$symb[48] = '/Ø/';
$symb[49] = '/ø/';
$symb[50] = '/Œ/';
$symb[51] = '/œ/';
$symb[52] = '/Š/';
$symb[53] = '/š/';
$symb[54] = '/ß/';
$symb[55] = '/Þ/';
$symb[56] = '/þ/';
$symb[57] = '/Ù/';
$symb[58] = '/ù/';
$symb[59] = '/Ú/';
$symb[60] = '/ú/';
$symb[61] = '/Û/';
$symb[62] = '/û/';
$symb[63] = '/Ü/';
$symb[64] = '/ü/';
$symb[65] = '/Ý/';
$symb[66] = '/ý/';
$symb[67] = '/Ÿ/';
$symb[68] = '/ÿ/';
$symb[69] = '/Ž/';
$symb[70] = '/ž/';

$repl = array();
$repl[0] = '&#192;';
$repl[1] = '&#224;';
$repl[2] = '&#193;';
$repl[3] = '&#225;';
$repl[4] = '&#194;';
$repl[5] = '&#226;';
$repl[6] = '&#195;';
$repl[7] = '&#227;';
$repl[8] = '&#196;';
$repl[9] = '&#228;';
$repl[10] = '&#197;';
$repl[11] = '&#229;';
$repl[12] = '&#198;';
$repl[13] = '&#230;';
$repl[14] = '&#199;';
$repl[15] = '&#231;';
$repl[16] = '&#272;';
$repl[17] = '&#208;';
$repl[18] = '&#240;';
$repl[19] = '&#200;';
$repl[20] = '&#232;';
$repl[21] = '&#201;';
$repl[22] = '&#233;';
$repl[23] = '&#202;';
$repl[24] = '&#234;';
$repl[25] = '&#203;';
$repl[26] = '&#235;';
$repl[27] = '&#402;';
$repl[28] = '&#204;';
$repl[29] = '&#236;';
$repl[30] = '&#205;';
$repl[31] = '&#237;';
$repl[32] = '&#206;';
$repl[33] = '&#238;';
$repl[34] = '&#207;';
$repl[35] = '&#239;';
$repl[36] = '&#209;';
$repl[37] = '&#241;';
$repl[38] = '&#210;';
$repl[39] = '&#242;';
$repl[40] = '&#211;';
$repl[41] = '&#243;';
$repl[42] = '&#212;';
$repl[43] = '&#244;';
$repl[44] = '&#213;';
$repl[45] = '&#245;';
$repl[46] = '&#214;';
$repl[47] = '&#246;';
$repl[48] = '&#216;';
$repl[49] = '&#248;';
$repl[50] = '&#338;';
$repl[51] = '&#339;';
$repl[52] = '&#352;';
$repl[53] = '&#353;';
$repl[54] = '&#223;';
$repl[55] = '&#222;';
$repl[56] = '&#254;';
$repl[57] = '&#217;';
$repl[58] = '&#249;';
$repl[59] = '&#218;';
$repl[60] = '&#250;';
$repl[61] = '&#219;';
$repl[62] = '&#251;';
$repl[63] = '&#220;';
$repl[64] = '&#252;';
$repl[65] = '&#221;';
$repl[66] = '&#253;';
$repl[67] = '&#159;';
$repl[68] = '&#255;';
$repl[69] = '&#381;';
$repl[70] = '&#382;';

ksort($symb);
ksort($repl);
?>

vincevincevince

2:41 pm on Jan 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That looks like a really useful script for when things have ended up all mixed up! For new builds however, absolutely set the page content encoding to utf8 and the database to the same to avoid all and any problems.