substr on UTF-8 strings returned from MySQL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • peter

    #1

    substr on UTF-8 strings returned from MySQL

    I am trying to truncate some Chinese Text returned from MySQL. If I use the
    substr function, then the last one or two chinese words would appear as
    symbols as opposed to the word it should be displaying? The PHP page itself
    is already UTF-8 encoded

    What is the best way of truncating such UTF-8 strings (e.g., to return the
    first 50 chinese "words"). Chinese words appears like
    "?????????????? ???????" (21 chinese "words" shown here).


  • Daniel Tryba

    #2
    Re: substr on UTF-8 strings returned from MySQL

    peter <peter@mail.co. uk> wrote:[color=blue]
    > I am trying to truncate some Chinese Text returned from MySQL. If I use the
    > substr function, then the last one or two chinese words would appear as
    > symbols as opposed to the word it should be displaying? The PHP page itself
    > is already UTF-8 encoded[/color]

    Behold: http://nl2.php.net/manual/en/ref.mbstring.php

    The multibyte string functions
    [color=blue]
    > What is the best way of truncating such UTF-8 strings (e.g., to return the
    > first 50 chinese "words"). Chinese words appears like
    > "?????????????? ???????" (21 chinese "words" shown here).[/color]

    mb_substr will do the trick....

    --

    Daniel Tryba

    Comment

    • Chung Leong

      #3
      Re: substr on UTF-8 strings returned from MySQL


      "Daniel Tryba" <news_comp.lang .php@canopus.nl > wrote in message
      news:celp5l$4pj $1@news.tue.nl. ..[color=blue]
      > peter <peter@mail.co. uk> wrote:[color=green]
      > > I am trying to truncate some Chinese Text returned from MySQL. If I use[/color][/color]
      the[color=blue][color=green]
      > > substr function, then the last one or two chinese words would appear as
      > > symbols as opposed to the word it should be displaying? The PHP page[/color][/color]
      itself[color=blue][color=green]
      > > is already UTF-8 encoded[/color]
      >
      > Behold: http://nl2.php.net/manual/en/ref.mbstring.php
      >
      > The multibyte string functions
      >[color=green]
      > > What is the best way of truncating such UTF-8 strings (e.g., to return[/color][/color]
      the[color=blue][color=green]
      > > first 50 chinese "words"). Chinese words appears like
      > > "?????????????? ???????" (21 chinese "words" shown here).[/color]
      >
      > mb_substr will do the trick....
      >
      > --
      >
      > Daniel Tryba
      >[/color]

      The PCRE engine of newer version of PHP is also capable of dealing with
      UTF-8 text. preg_match('/(.{1,50})/u', $s, $m) would yield a string of 50
      characters.


      Comment

      Working...