In the opinion of PHP, what is a character?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lkrubner@geocities.com

    In the opinion of PHP, what is a character?




    I'm worried about idiot users that write long essays in Microsoft Word,
    then log into their accounts and bring up an HTML form and copy and
    paste the essay and hit submit. Or perhaps they do this using
    WordPerfect. Or perhaps they use MacWrite.

    I output everything from my site as UTF-8. I'd like to check the input
    for characters that are not UTF-8 and then turn the bad ones to an
    ASCII question mark. I could loop through a string as if it was an
    array and test each character, but what does PHP think a character is?
    Does PHP understand what a multi-byte character is? Would this work?


    // the $string is a form input, possibly containing characters
    // written in any of the world's word processors
    $finalString = "";
    for ($i=0; $i < strlen($string) ; $i++) {
    $char = $string[$i];
    $encoding = mb_detect_encod ing($char);
    if ($encoding != "UTF-8") {
    $char = "?";
    }
    $finalString .= $char;
    }



    They offer this on www.php.net, in the comments, but, again, I'm not
    sure it would work on individual characters, and I'm about reading
    Regx.



    =============== ============

    Much simpler UTF-8-ness checker using a regular expression created by
    the W3C:

    <?php

    // Returns true if $string is valid UTF-8 and false otherwise.
    function is_utf8($string ) {

    // From http://w3.org/International/question...rms-utf-8.html
    return preg_match('%^( ?:
    [\x09\x0A\x0D\x2 0-\x7E] # ASCII
    | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
    | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
    | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
    | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
    | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
    | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
    | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
    )*$%xs', $string);

    } // function is_utf8

    ?>

  • Dana Cartwright

    #2
    Re: In the opinion of PHP, what is a character?

    <lkrubner@geoci ties.com> wrote in message
    news:1121930994 .203756.257720@ g49g2000cwa.goo glegroups.com.. .
    [color=blue]
    > what does PHP think a character is?
    > Does PHP understand what a multi-byte character is?[/color]

    The user's manual is your friend here...

    "In PHP, a character is the same as a byte, that is, there are exactly 256
    different characters possible. This also implies that PHP has no native
    support of Unicode. See utf8_encode() and utf8_decode() for some Unicode
    support."

    quoted from




    Comment

    • Andy Hassall

      #3
      Re: In the opinion of PHP, what is a character?

      On 21 Jul 2005 00:29:54 -0700, lkrubner@geocit ies.com wrote:
      [color=blue]
      >I output everything from my site as UTF-8. I'd like to check the input
      >for characters that are not UTF-8 and then turn the bad ones to an
      >ASCII question mark.[/color]

      Déja vu all over again. ;-p
      [color=blue]
      >I could loop through a string as if it was an
      >array and test each character, but what does PHP think a character is?[/color]

      PHP's string data type has no knowledge of character encodings. It treats
      strings as a meaning-free series of bytes. Not characters.
      [color=blue]
      >Does PHP understand what a multi-byte character is?[/color]

      No. The mbstring extension does, though.
      [color=blue]
      >Would this work?
      >
      >// the $string is a form input, possibly containing characters
      >// written in any of the world's word processors
      >$finalString = "";
      >for ($i=0; $i < strlen($string) ; $i++) {
      > $char = $string[$i];
      > $encoding = mb_detect_encod ing($char);
      > if ($encoding != "UTF-8") {
      > $char = "?";
      > }
      > $finalString .= $char;
      >}[/color]

      No. This goes byte-by-byte. There's no reason why mb_detect_encod ing should
      return UTF-8, since for anything <127 then it could equally be ASCII, or for
      other values some other encoding such as ISO-8859-15.

      To find invalid UTF-8 encoded byte sequences you have to consider more than
      one byte at a time.

      As I believe was covered the previous times you've asked this:

      You can tell whether a series of bytes is not a series of UTF-8 encoded
      characters, by looking for byte sequences that are not valid UTF-8 - look for
      lead bytes and the corresponding numbers of trail bytes.

      Therefore, your current request (replace byte sequences that cannot be UTF-8
      encodings with a "?" character) is quite possible, but you need to consider
      more than one byte at a time and will probably have to backtrack a bit if you
      get an invalid sequence.

      In one previous incarnation of this thread I posted a script to detect invalid
      UTF-8 byte sequences; looks like this could be quite easily adapted to your
      current request:



      Just remove the various returns that exit when it finds bad characters, and
      instead, whenever $charSize drops to zero, append it to a string for output, or
      if it finds a bad encoing, append a "?".

      However, you cannot tell whether a byte sequence is actually a series of UTF-8
      characters, because it could be encoded in something else that happens to share
      the same byte representation.

      --
      Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
      <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

      Comment

      • lkrubner@geocities.com

        #4
        Re: In the opinion of PHP, what is a character?

        >>I output everything from my site as UTF-8. I'd like to check the input[color=blue][color=green]
        >>for characters that are not UTF-8 and then turn the bad ones to an
        >>ASCII question mark.[/color]
        >
        >Déja vu all over again. ;-p[/color]

        I know. Every 3 or 4 months I come back to the issue and try to fix it,
        but I never get it fixed. This has been going on for 2 years now. Maybe
        I'll get it this time.

        Comment

        • Chung Leong

          #5
          Re: In the opinion of PHP, what is a character?

          This should work as well:

          preg_match('/./u', $text);

          Badly encoded UTF-8 text would cause PCRE to go poopoo and return false
          even where the string isn't empty.

          Comment

          • Umberto Salsi

            #6
            Re: In the opinion of PHP, what is a character?

            lkrubner@geocit ies.com wrote:
            [color=blue]
            > I output everything from my site as UTF-8. I'd like to check the input
            > for characters that are not UTF-8 and then turn the bad ones to an
            > ASCII question mark. [...][/color]

            This function simply drop all the illegal sequences and return a legal
            UTF-8 string:

            function Force_UTF_8($s)
            {
            return mb_convert_enco ding($s, 'UTF-8', 'UTF-8');
            }

            For example:

            Force_UTF_8("A\ x00B\xc0\x80C") ==> "A\000BC"

            Note that the control characters (here \000) aren't removed.

            Should be enought for many cases. You may send a warning to the user if
            the resulting string differ from the original one.

            Regards,
            ___
            /_|_\ Umberto Salsi
            \/_\/ www.icosaedro.it

            Comment

            Working...