Connecting Tech Pros Worldwide Help | Site Map

Convert text

  #1  
Old June 28th, 2006, 03:45 PM
ngocviet
Guest
 
Posts: n/a
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, 04:15 PM
Ruben van Engelenburg
Guest
 
Posts: n/a

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, 04:45 PM
ngocviet
Guest
 
Posts: n/a

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, 10:55 PM
Chung Leong
Guest
 
Posts: n/a

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, 03:15 AM
ngocviet
Guest
 
Posts: n/a

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, 03:35 PM
Chung Leong
Guest
 
Posts: n/a

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, 12:15 PM
Kimmo Laine
Guest
 
Posts: n/a

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, 04:15 AM
ngocviet
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert text, from english to other language using asp.net rajendiran answers 1 December 6th, 2007 08:01 PM
Convert text encoded with character referense (&#123;) to unicode or uft-8 Daniel Kster answers 4 November 22nd, 2005 11:43 AM
Can't convert Text string to an int32 using Convert.Int32 Convert TextBox.Text to Int32 Problem answers 3 November 16th, 2005 03:41 AM
Convert text encoded with character referense (&#123;) to unicode or uft-8 Daniel Kster answers 4 July 21st, 2005 03:54 PM
Convert text to double Erwin Bormans answers 3 July 17th, 2005 10:30 PM