note 40719 deleted from function.strtr by nlopess

From: Date: Fri, 12 Mar 2004 14:50:00 +0000
Subject: note 40719 deleted from function.strtr by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-66737@lists.php.net to get a copy of this message
Note Submitter: e d _ x m g at arjih (reverse) ---- 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 ?>

« previous php.notes (#66737) next »