For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > December 2006 > Character encoding problems (I think)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Character encoding problems (I think)
Dannii

2006-12-11, 7:01 pm

I am using the following function in a project of mine. When I placed
it in a test file it ran fine.
However when I try to use it with text I get from a database, the
function returns an empty string. This is even with plain un-accented
characters. I think is probably due to encoding issues, as the database
table is encoded as utf8_general_ci. How can I make in_array with utf8?

code:
$characterHash =3D array ( 'a' =3D> array ('a', 'A', '=E0', '=C0', '=E1', '=C1', '=E2', '=C2', '=E3', '=C3', '=E4', '=C4', '=E5', '=C5', '=AA'), 'b' =3D> array ('b', 'B'), 'c' =3D> array ('c', 'C', '=E7', '=C7'), 'd' =3D> array ('d', 'D', '=D0'), 'e' =3D> array ('e', 'E', '=E8', '=C8', '=E9', '=C9', '=EA', '=CA', '=EB', '=CB'), 'f' =3D> array ('f', 'F'), 'g' =3D> array ('g', 'G'), 'h' =3D> array ('h', 'H'), 'i' =3D> array ('i', 'I', '=EC', '=CC', '=ED', '=CD', '=EE', '=CE', '=EF', '=CF'), 'j' =3D> array ('j', 'J'), 'k' =3D> array ('k', 'K'), 'l' =3D> array ('l', 'L'), 'm' =3D> array ('m', 'M'), 'n' =3D> array ('n', 'N', '=F1', '=D1'), 'o' =3D> array ('o', 'O', '=F2', '=D2', '=F3', '=D3', '=F4', '=D4', '=F5', '=D5', '=F6', '=D6', '=F8', '=D8', '=BA'), 'p' =3D> array ('p', 'P'), 'q' =3D> array ('q', 'Q'), 'r' =3D> array ('r', 'R', '=AE'), 's' =3D> array ('s', 'S', '=DF'), 't' =3D> array ('t', 'T'), 'u' =3D> array ('u', 'U', '=F9', '=D9', '=FA', '=DA', '=FB', '=DB', '=FC', '=DC', '=B5'), 'v' =3D> array ('v', 'V'), 'w' =3D> array ('w', 'W'), 'x' =3D> array ('x', 'X', '=D7'), 'y' =3D> array ('y', 'Y', '=FD', '=DD', '=FF'), 'z' =3D> array ('z', 'Z'), '-' =3D> array ('-', ' '), '_' =3D> array ('_'), '+' =3D> array ('+', '&'), '+-' =3D> array ('=B1'), '0' =3D> array ('0'), '1' =3D> array ('1', '=B9'), '2' =3D> array ('2', '=B2'), '3' =3D> array ('3', '=B3'), '4' =3D> array ('4'), '5' =3D> array ('5'), '6' =3D> array ('6'), '7' =3D> array ('7'), '8' =3D> array ('8'), '9' =3D> array ('9'), 'ae' =3D> array ('=E6', '=C6'), 'at' =3D> array ('@'), 'cent' =3D> array ('=A2'), 'copyright' =3D> array ('=A9'), 'degrees' =3D> array ('=B0'), 'dollar' =3D> array ('$'), 'half' =3D> array ('=BD'), 'percent' =3D> array ('%'), 'pound' =3D> array ('=A3'), 'quarter' =3D> array ('=BC'), 'section' =3D> array ('=A7'), 'three-quarters' =3D> array ('=BE'), 'yen' =3D> array ('=A5'), ); function transformText($text) { global $characterHash; $prettytext =3D ''; for ($i =3D 0; $i < strlen($text); $i++) { foreach ($characterHash as $replace =3D> $search) { if (in_array(substr($text, $i, 1), $search)) { $prettytext .=3D $replace; break; } } } return $prettytext; }


petersprc

2006-12-11, 7:01 pm

Hi,

Maybe using mbstring will help? Some tips:

http://webcollab.sourceforge.net/unicode.html

Dannii wrote:
> I am using the following function in a project of mine. When I placed
> it in a test file it ran fine.
> However when I try to use it with text I get from a database, the
> function returns an empty string. This is even with plain un-accented
> characters. I think is probably due to encoding issues, as the database
> table is encoded as utf8_general_ci. How can I make in_array with utf8?
>
>
code:
> $characterHash =3D array ( > 'a' =3D> array ('a', 'A', '=E0', '=C0', '=E1', '=C1', '=E2', '=C2', '=E3=
', > '=C3', '=E4', '=C4', '=E5', '=C5', '=AA'), > 'b' =3D> array ('b', 'B'), > 'c' =3D> array ('c', 'C', '=E7', '=C7'), > 'd' =3D> array ('d', 'D', '=D0'), > 'e' =3D> array ('e', 'E', '=E8', '=C8', '=E9', '=C9', '=EA', '=CA', '=EB= ', > '=CB'), > 'f' =3D> array ('f', 'F'), > 'g' =3D> array ('g', 'G'), > 'h' =3D> array ('h', 'H'), > 'i' =3D> array ('i', 'I', '=EC', '=CC', '=ED', '=CD', '=EE', '=CE', '=EF= ', > '=CF'), > 'j' =3D> array ('j', 'J'), > 'k' =3D> array ('k', 'K'), > 'l' =3D> array ('l', 'L'), > 'm' =3D> array ('m', 'M'), > 'n' =3D> array ('n', 'N', '=F1', '=D1'), > 'o' =3D> array ('o', 'O', '=F2', '=D2', '=F3', '=D3', '=F4', '=D4', '=F5= ', > '=D5', '=F6', '=D6', '=F8', '=D8', '=BA'), > 'p' =3D> array ('p', 'P'), > 'q' =3D> array ('q', 'Q'), > 'r' =3D> array ('r', 'R', '=AE'), > 's' =3D> array ('s', 'S', '=DF'), > 't' =3D> array ('t', 'T'), > 'u' =3D> array ('u', 'U', '=F9', '=D9', '=FA', '=DA', '=FB', '=DB', '=FC= ', > '=DC', '=B5'), > 'v' =3D> array ('v', 'V'), > 'w' =3D> array ('w', 'W'), > 'x' =3D> array ('x', 'X', '=D7'), > 'y' =3D> array ('y', 'Y', '=FD', '=DD', '=FF'), > 'z' =3D> array ('z', 'Z'), > '-' =3D> array ('-', ' '), > '_' =3D> array ('_'), > '+' =3D> array ('+', '&'), > '+-' =3D> array ('=B1'), > '0' =3D> array ('0'), > '1' =3D> array ('1', '=B9'), > '2' =3D> array ('2', '=B2'), > '3' =3D> array ('3', '=B3'), > '4' =3D> array ('4'), > '5' =3D> array ('5'), > '6' =3D> array ('6'), > '7' =3D> array ('7'), > '8' =3D> array ('8'), > '9' =3D> array ('9'), > 'ae' =3D> array ('=E6', '=C6'), > 'at' =3D> array ('@'), > 'cent' =3D> array ('=A2'), > 'copyright' =3D> array ('=A9'), > 'degrees' =3D> array ('=B0'), > 'dollar' =3D> array ('$'), > 'half' =3D> array ('=BD'), > 'percent' =3D> array ('%'), > 'pound' =3D> array ('=A3'), > 'quarter' =3D> array ('=BC'), > 'section' =3D> array ('=A7'), > 'three-quarters' =3D> array ('=BE'), > 'yen' =3D> array ('=A5'), > ); > > > function transformText($text) > { > global $characterHash; > > $prettytext =3D ''; > for ($i =3D 0; $i < strlen($text); $i++) > { > foreach ($characterHash as $replace =3D> $search) > { > if (in_array(substr($text, $i, 1), $search)) > { > $prettytext .=3D $replace; > break; > } > } > } > return $prettytext; > } >


Dannii

2006-12-11, 7:01 pm

Solved it (and other problems)

Using mb_strlen and mb_substr worked. Thanks

petersprc wrote:
> Hi,
>
> Maybe using mbstring will help? Some tips:
>
> http://webcollab.sourceforge.net/unicode.html


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com