Convert text

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

    Convert text

    I have a text like this: "Thuyền Và Biển"
    How to convert it to: "Thuyền Và Biển"
    Please help!

  • Ruben van Engelenburg

    #2
    Re: Convert text

    ngocviet wrote:[color=blue]
    > I have a text like this: "Thuyền Và Biển"
    > How to convert it to: "Thuyền Và Biển"
    > Please help!
    >[/color]

    This is the same question that McHenry asked today.
    The answer is simple: use html_entity_dec ode():


    <?php
    echo html_entity_dec ode('Thuyền V&#224; Biển');
    ?>

    HTH.
    Ruben.

    Comment

    • ngocviet

      #3
      Re: Convert text

      It work when out in brower! But when I save to file, nothing change! :)


      Ruben van Engelenburg wrote:[color=blue]
      > This is the same question that McHenry asked today.
      > The answer is simple: use html_entity_dec ode():
      >
      >
      > <?php
      > echo html_entity_dec ode('Thuyền V&#224; Biển');
      > ?>
      >
      > HTH.
      > Ruben.[/color]

      Comment

      • Chung Leong

        #4
        Re: Convert text


        ngocviet wrote:[color=blue]
        > I have a text like this: "Thuyền V&#224; Biển"
        > How to convert it to: "Thuyền Và Biển"
        > Please help![/color]

        What text char-set/encoding do you want the text to be?

        Comment

        • ngocviet

          #5
          Re: Convert text

          I want to convert it to UTF-8 encoding


          Chung Leong wrote:[color=blue]
          > ngocviet wrote:[color=green]
          > > I have a text like this: "Thuyền V&#224; Biển"
          > > How to convert it to: "Thuyền Và Biển"
          > > Please help![/color]
          >
          > What text char-set/encoding do you want the text to be?[/color]

          Comment

          • Chung Leong

            #6
            Re: Convert text


            ngocviet wrote:[color=blue]
            > I want to convert it to UTF-8 encoding
            >
            >
            > Chung Leong wrote:[color=green]
            > > ngocviet wrote:[color=darkred]
            > > > I have a text like this: "Thuyền V&#224; Biển"
            > > > How to convert it to: "Thuyền Và Biển"
            > > > Please help![/color]
            > >
            > > What text char-set/encoding do you want the text to be?[/color][/color]

            You will have to do it manually using preg_replace_ca llback(), as I
            don't believe html_entity_dec ode() is capable of decoding such
            entities. Here're the basics:

            $text = preg_replace_ca llback('/&#(d+);/',
            'numeric_html_e ntity_replace', $text);

            function numeric_html_en tity_replace($m ) {
            $unicode = (int) $m[1];
            $utf8 = /* encode the Uncode value */
            return $utf8
            }

            The tricky part is converting the Unicode value to a UTF-8 string.

            Comment

            • Kimmo Laine

              #7
              Re: Convert text

              "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
              news:1151591566 .326139.249300@ y41g2000cwy.goo glegroups.com.. .[color=blue]
              >
              > You will have to do it manually using preg_replace_ca llback(), as I
              > don't believe html_entity_dec ode() is capable of decoding such
              > entities. Here're the basics:
              >
              > $text = preg_replace_ca llback('/&#(d+);/',
              > 'numeric_html_e ntity_replace', $text);
              >
              > function numeric_html_en tity_replace($m ) {
              > $unicode = (int) $m[1];
              > $utf8 = /* encode the Uncode value */
              > return $utf8
              > }
              >
              > The tricky part is converting the Unicode value to a UTF-8 string.[/color]

              There's a solution for that at php.net



              chr seems to be ASCII-only (which is kinda stupid if you think about it,
              plenty of utf implementations these days) but "grey" had written a
              workaround for it.


              --
              "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
              spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)



              Comment

              • ngocviet

                #8
                Re: Convert text

                Thanks Kimmo Laine!
                It solves my problem!
                Code:

                function unichr($dec)
                {
                if ($dec < 128) {
                $utf = chr($dec);
                } else if ($dec < 2048) {
                $utf = chr(192 + (($dec - ($dec % 64)) / 64));
                $utf .= chr(128 + ($dec % 64));
                } else {
                $utf = chr(224 + (($dec - ($dec % 4096)) / 4096));
                $utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
                $utf .= chr(128 + ($dec % 64));
                }
                return $utf;
                }

                $str = "Thuyền V&#224; Biển";
                $str = preg_replace("/&#(\d{2,5});/e", "unichr($1) ;", $str);
                echo $str;



                Kimmo Laine wrote:
                There's a solution for that at php.net
                >

                >
                chr seems to be ASCII-only (which is kinda stupid if you think about it,
                plenty of utf implementations these days) but "grey" had written a
                workaround for it.
                >
                >
                --
                "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
                spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)

                Comment

                Working...