note 64645 deleted from function.html-entity-decode by bjori
| From: | bjori@php.net | Date: | Wed, 19 Apr 2006 12:22:17 +0000 |
| Subject: | note 64645 deleted from function.html-entity-decode by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-108597@lists.php.net to get a copy of this message | ||
Note Submitter: carlosdelriosilvan at yahoo dot es
Reason: bad code
----
This one is intended for those who want to convert special characters into their corresponding
normal characters, for instance an "é" into an "e", or a "ç"
into a "c", or a Spanish "ñ" into an "n".
It is aimed at converting a string full of special characters into a string that might be valid as
an URL or as a part of it.
It might not work for a few characters, but it will for those most usual in Spanish, French,
Italian, Portuguese, Catalan, etc.
function strtourl ($noentities) {
// We first 'clean' the string to something similar to an URL
// (These lines are not necessary if what you want is just to strip the special characters)
$noentities = htmlentities (strtolower (trim ($noentities)));
$noentities = preg_replace ("/\s+/", "", $noentities);
// Then we search the entities in it
preg_match_all ('/[&]+[\w]+[;]+/i', $noentities, $entitiesfound);
// Now we replace anything found between a '&' and a ';' with the first
character found after the '&'
foreach ($entitiesfound[0] as $specialchar) {
$noentities = str_replace ($specialchar, substr ($specialchar, 1, 1), $noentities);
}
return $noentities;
}
// Example (in Castilian Spanish)
print strtourl ("FÃjate tú qué año");
I am an amateur programmer - if programmer at all! - so forgive my possible errors