Connecting Tech Pros Worldwide Forums | Help | Site Map

Convert text

ngocviet
Guest
 
Posts: n/a
#1: Jun 28 '06
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
Guest
 
Posts: n/a
#2: Jun 28 '06

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.
ngocviet
Guest
 
Posts: n/a
#3: Jun 28 '06

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]

Chung Leong
Guest
 
Posts: n/a
#4: Jun 28 '06

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?

ngocviet
Guest
 
Posts: n/a
#5: Jun 29 '06

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]

Chung Leong
Guest
 
Posts: n/a
#6: Jun 29 '06

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.

Kimmo Laine
Guest
 
Posts: n/a
#7: Jun 30 '06

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)



ngocviet
Guest
 
Posts: n/a
#8: Jul 2 '06

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)
Closed Thread