Click to See Complete Forum and Search --> : preg_replace problem
gizmo
05-24-2005, 03:40 PM
I want to use the function posted in the php manual under strtr on 13-Apr-2005. However it does not behave as described. Not only does it not remove the accents, it replaces every space with a hyphen. Since I have only a vague knowledge of regular expressions, I do not know how to fix it. I want to convert all strings of the form &xaccent; to just x and then all remaining &---; to a single '-' (or preferably '#').
ShrineDesigns
05-24-2005, 11:59 PM
regexp pattern&[^&;]+;
gizmo
05-25-2005, 06:52 AM
Thanks, that's solved the second part of the problem, but what about
$string = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml);/","$1",$string);
This is supposed to remove accented characters and replace them with the non-accented equivalent?
ShrineDesigns
05-25-2005, 05:20 PM
huh???
if you want to convert an entity to its non-entity character, use html_entity_decode() (http://www.php.net/manual/en/function.html-entity-decode.php)
gizmo
05-26-2005, 06:54 AM
No, I don't. I want to convert it to just the character, no accents at all, so that 'î' becomes simply 'i'
gizmo
05-27-2005, 09:54 AM
I have looked at a number of regex tutorials and AFAIK the "$1" part is incorrect, but I fail to see why. Any offers ?
NogDog
05-27-2005, 12:59 PM
I just tried this and it worked fine:
$string = "é ç ô è Å ñ ü";
echo "<p>$string</p>\n";
$string = preg_replace("/&(.)(acute|cedil|circ|grave|ring|tilde|uml);/", "$1", $string);
echo "<p>$string</p>\n";
gizmo
05-27-2005, 01:52 PM
I agree but it doesn't seem to work inside the original function.
function getRewriteString($string)
{ $string = htmlentities($string);
$string = preg_replace("/&(.)(acute|cedil|circ|grave|ring|tilde|uml);/", "$1", $string);
$string = preg_replace("/&[^&;]+;/", "#", html_entity_decode($string));
return $string;
}