Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts

    PHP and Currency Symbols (displaying special chars nightmare)

    HELP

    I'm having a nightmare handling currency special chars.

    I'm using the class below to convert inputted currency symblls into the NCR code for that symbol.

    PHP Code:
    class unicode_replace_entities {
            public function 
    UTF8entities($content="") {
                
    $contents $this->unicode_string_to_array($content);
                
    $swap "";
                
    $iCount count($contents);
                for (
    $o=0;$o<$iCount;$o++) {
                    
    $contents[$o] = $this->unicode_entity_replace($contents[$o]);
                    
    $swap .= $contents[$o];
                }
                return 
    mb_convert_encoding($swap,"UTF-8"); //not really necessary, but why not.
            
    }

            public function 
    unicode_string_to_array$string ) { //adjwilli
                
    $strlen mb_strlen($string);
                while (
    $strlen) {
                    
    $array[] = mb_substr$string01"UTF-8" );
                    
    $string mb_substr$string1$strlen"UTF-8" );
                    
    $strlen mb_strlen$string );
                }
                return 
    $array;
            }

            public function 
    unicode_entity_replace($c) { //m. perez
                
    $h ord($c{0});   
                if (
    $h <= 0x7F) {
                    return 
    $c;
                } else if (
    $h 0xC2) {
                    return 
    $c;
                }
               
                if (
    $h <= 0xDF) {
                    
    $h = ($h 0x1F) << | (ord($c{1}) & 0x3F);
                    
    $h "&#" $h ";";
                    return 
    $h;
                } else if (
    $h <= 0xEF) {
                    
    $h = ($h 0x0F) << 12 | (ord($c{1}) & 0x3F) << | (ord($c{2}) & 0x3F);
                    
    $h "&#" $h ";";
                    return 
    $h;
                } else if (
    $h <= 0xF4) {
                    
    $h = ($h 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12 | (ord($c{2}) & 0x3F) << | (ord($c{3}) & 0x3F);
                    
    $h "&#" $h ";";
                    return 
    $h;
                }
            }
        } 
    Now this code works fine if I just immediately spit the encoded string back out to the page using echo. however I want to store it all in a database I simply insert the data into the DB table and I end up with this
    Code:
    ID 	ShortCode 	Symbol
    1 	GBP 	& #163
    2 	USD 	$
    3 	Euro 	& #836
    4 	BGN 	& #108
    5 	CNY 	& #165
    6 	CRC 	& #835
    7 	CUP 	& #836
    8 	INR 	& #836
    Which is exactly right.
    However now when I pull from the DB to echo out back to screen isntead of echoing out the special chars correctly as it did before it now no longer spits out anything apart from the Yen, Pound and Dollar sign but it is echoing out the right NCR code just isn't being displayed (however it was being displayed correctly before insert to db)

    See attachment for screenshot of what it looks like after db retreval.

    Code:
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    is set on the page.
    Attached Thumbnails Attached Thumbnails PHP and Currency Symbols (displaying special chars nightmare)-image1.jpg  
    Last edited by hinch; 02-11-2010 at 01:56 PM.
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    maybe the used font doesn’t contain the currency glyphs? check out the source code whether the entities were printed.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    yeah it does contain them because it displays them fine echoing them out before db insertion its only on db retreval that they don't display correctly
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    erm, are the entities in the HTML source code or not?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •