www.webdeveloper.com
Results 1 to 3 of 3

Thread: Convert foreign characters to HTML with PHP

  1. #1
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,097

    Convert foreign characters to HTML with PHP

    I am trying to build a function to extract the Facebook ID of a user.

    The simple part of my function works fine, but I would like to display the user's name as well - this breaks down when foreign characters are displayed.

    You can see the test form here:
    http://www.metataggenerator.org/anal...-id-finder.php

    This is already populated with a foreign character Facebook ID.

    I would like to find some way to globally switch this to HTML, in other words change this:
    HTML Code:
    \u0105
    into this:
    Code:
    ą
    This would then cover every possible FB language... Spanish, Polish, Greek, Russian...

    To go through all of the characters one at a time seems silly - like this:
    HTML Code:
    $description = ereg_replace('\u0142','ł', $description);

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    Off hand I don't know an easy way to do this, though you could probably do something with preg_replace_callback(), using a character class that is a range of UTF-8 character codes above decimal 255. But my first thought is, why bother? Why not just serve up the page as UTF-8 and not worry about it?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,097
    This works if there were some way to shorten it to a function... say, for X =...

    PHP Code:
    <?php
    $idurl 
    'http://www.facebook.com/tkliwinihilisci';
    $page file_get_contents("https://graph.facebook.com/$idurl");
    $string $page;

    $ent = array(             // Convert Foreign Language Characters to HTML
    '\u011'=>'&#x011;',
    '\u012'=>'&#x012;',
    '\u013'=>'&#x013;',
    '\u014'=>'&#x014;',
    '\u016'=>'&#x016;',
    '\u017'=>'&#x017;',
    '\u018'=>'&#x018;',
    '\u019'=>'&#x019;',
    '\u020'=>' ',
    '\u015b'=>'&#x015b;',
    '\u0105'=>'&#x0105;',
    '\u0106'=>'&#x0106;',
    '\u0107'=>'&#x0107;',
    '\u0108'=>'&#x0108;',
    '\u0109'=>'&#x0109;',
    '\u0110'=>'&#x0110;',
    '\u0111'=>'&#x0111;',
    '\u0112'=>'&#x0112;',
    '\u0113'=>'&#x0113;',
    '\u0114'=>'&#x0114;',
    '\u0115'=>'&#x0115;',
    '\u0116'=>'&#x0116;',
    '\u0117'=>'&#x0117;',
    '\u0118'=>'&#x0118;',
    '\u0119'=>'&#x0119;',
    '\u0120'=>'&#x0120;',
    '\u0121'=>'&#x0121;',
    '\u0122'=>'&#x0122;',
    '\u0123'=>'&#x0123;',
    '\u0124'=>'&#x0124;',
    '\u0125'=>'&#x0125;',
    '\u0126'=>'&#x0126;',
    '\u0127'=>'&#x0127;',
    '\u0128'=>'&#x0128;',
    '\u0129'=>'&#x0129;',
    '\u0439'=>'&#x0439;'
    ); 
    $string strtr("$string"$ent);
    echo 
    $string;
    ?>
    Last edited by donatello; 07-12-2011 at 05:20 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles