note 64645 added to function.html-entity-decode
| From: | carlosdelriosilvan at yahoo dot es | Date: | Wed, 19 Apr 2006 12:16:03 +0000 |
| Subject: | note 64645 added to function.html-entity-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-108588@lists.php.net to get a copy of this message | ||
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
----
Server IP: 217.174.203.10
Probable Submitter: 147.83.3.43
----
X-Spam-Status: Yes, hits=7.4 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HEAD_ILLEGAL_CHARS autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.html-entity-decode.php
Edit -- http://master.php.net/note/edit/64645
Del: integrated -- http://master.php.net/note/delete/64645/integrated
Del: useless -- http://master.php.net/note/delete/64645/useless
Del: bad code -- http://master.php.net/note/delete/64645/bad+code
Del: spam -- http://master.php.net/note/delete/64645/spam
Del: non-english -- http://master.php.net/note/delete/64645/non-english
Del: in docs -- http://master.php.net/note/delete/64645/in+docs
Del: other reasons-- http://master.php.net/note/delete/64645
Reject -- http://master.php.net/note/reject/64645
Search -- http://master.php.net/manage/user-notes.php