note 40719 added to function.strtr
| From: | (reverse) | Date: | Fri, 12 Mar 2004 09:22:04 +0000 |
| Subject: | note 40719 added to function.strtr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-66700@lists.php.net to get a copy of this message | ||
Here is a functino that helps you to match even Umlauts in a case-insensitive Regular Expression.
Usually, "über" would match only "über", not "Über".
So you would have to match for "[Üü]ber".
This function would do it:
<?
// Source for Translation-Table
$myTrans = array();
$trans =
'ÿÀàÁáÂâÃãÄäÅåÇçÈèÉéÊêËëÌìÍíÎîÏïÑñÒòÓóÔôÕõÖöØøÙùÚúÛûÜüÝýÆæ';
// Create a translation-Table:
// 'ä' => '[Ää']', 'Ä' => '[Ää]'
// ... and so on ...
function createTranslationTable($trans) {
$myTrans = array();
for ($i=0; $i<strlen($trans); $i+=2) {
$a = $trans{$i};
$b = $trans{$i+1};
$regExp = "[".$a.$b."]";
$myTrans[$a] = $regExp;
$myTrans[$b] = $regExp;
}
return $myTrans;
}
// Should be called just once before heavy use:
$myTrans = createTranslationTable($trans);
// The function that makes the string REALLY
// case-insensitive matchable:
function preg_quote_ext($str) {
global $myTrans;
return strtr($str,$myTrans);
}
// Our text for testing:
$name = "Her name was Móñïçâ, which was indeed very strange.";
$preg_name = preg_quote_ext($name);
// Matches lowercase and uppercase with accents:
return preg_match("/$preg_name/","Móñïçâ"); // -> true
return preg_match("/$preg_name/","MÓÑÏÇÂ"); // -> true
// The accents are still important for a match:
return preg_match("/$preg_name/","MÒÑÏÇÁ"); // -> false
?>
----
Manual Page -- http://www.php.net/manual/en/function.strtr.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40719
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40719&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40719&report=yes
Search -- http://master.php.net/manage/user-notes.php