...

View Full Version : convert to html



urgido
08-29-2006, 06:23 AM
I have the next code:


$mail = str_replace("", "á", $mail);

This replace all characters in corresponding html code. But How I can replace all html characters without put each by each replace sentence?

kehers
08-29-2006, 10:56 AM
If you knwo the exact html characters you want to replace, u can store them up in an array, with their replacemnts in another array and then use preg_replace, this way:


//patterns to match
$pat[0] = "/@/";
$pat[1] = "/á/";
//and so on
//then the replacements
$rep[0] = "@";
$rep[1] = "á";
//Replace
$mail = preg_replace($pat, $rep, $mail);

if you however want all html special characters within the string mail to be automatically translated to the normal meaning, use this


$mail = html_entity_decode($mail);

hey, echo out $mail after u try this to be sure it gave u wat u are looking for.

chump2877
08-29-2006, 11:00 AM
I have the next code:


$mail = str_replace("", "á", $mail);
This replace all characters in corresponding html code. But How I can replace all html characters without put each by each replace sentence?

I guess you gave up on using htmlentities() (http://us2.php.net/manual/en/function.htmlentities.php)? The third parameter of htnlentities allows you to choose a character set as well, if thats an issue....

However, another way is to put all of your characters into an array, then iterate through the array, like so:


$char_array = array('á' => '','replace2' => 'character2', 'replace3' => 'character3');
foreach ($char_array as $key => $val)
{
$mail = str_replace($val, $key, $mail);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum