Connecting Tech Pros Worldwide Help | Site Map

Convert text

 
LinkBack Thread Tools Search this Thread
  #1  
Old June 28th, 2006, 02:45 PM
ngocviet
Guest
 
Posts: n/a
Default 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!


  #2  
Old June 28th, 2006, 03:15 PM
Ruben van Engelenburg
Guest
 
Posts: n/a
Default 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_decode():


<?php
echo html_entity_decode('Thuyền Và Biển');
?>

HTH.
Ruben.
  #3  
Old June 28th, 2006, 03:45 PM
ngocviet
Guest
 
Posts: n/a
Default 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_decode():
>
>
> <?php
> echo html_entity_decode('Thuyền Và Biển');
> ?>
>
> HTH.
> Ruben.[/color]

  #4  
Old June 28th, 2006, 09:55 PM
Chung Leong
Guest
 
Posts: n/a
Default 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]

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

  #5  
Old June 29th, 2006, 02:15 AM
ngocviet
Guest
 
Posts: n/a
Default 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à 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]

  #6  
Old June 29th, 2006, 02:35 PM
Chung Leong
Guest
 
Posts: n/a
Default 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à 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_callback(), as I
don't believe html_entity_decode() is capable of decoding such
entities. Here're the basics:

$text = preg_replace_callback('/&#(d+);/',
'numeric_html_entity_replace', $text);

function numeric_html_entity_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.

  #7  
Old June 30th, 2006, 11:15 AM
Kimmo Laine
Guest
 
Posts: n/a
Default Re: Convert text

"Chung Leong" <chernyshevsky@hotmail.com> wrote in message
news:1151591566.326139.249300@y41g2000cwy.googlegr oups.com...[color=blue]
>
> You will have to do it manually using preg_replace_callback(), as I
> don't believe html_entity_decode() is capable of decoding such
> entities. Here're the basics:
>
> $text = preg_replace_callback('/&#(d+);/',
> 'numeric_html_entity_replace', $text);
>
> function numeric_html_entity_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

http://fi.php.net/manual/en/function.chr.php#55978

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@bhgbyrzcv.arg)



  #8  
Old July 2nd, 2006, 03:15 AM
ngocviet
Guest
 
Posts: n/a
Default 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à Biển";
$str = preg_replace("/&#(\d{2,5});/e", "unichr($1);", $str);
echo $str;



Kimmo Laine wrote:
Quote:
There's a solution for that at php.net
>
http://fi.php.net/manual/en/function.chr.php#55978
>
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@bhgbyrzcv.arg)
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.