note 110637 added to function.in-array

From: Date: Thu, 15 Nov 2012 15:43:38 +0000
Subject: note 110637 added to function.in-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-191999@lists.php.net to get a copy of this message
This code to return true or false on SQL LIKE pattern (wildcard) search in array: //Example //Specify haystack $array = "House,Car,Boat,Horse,Pool Boy"; //Specify sql type like wildcard: %ou% $needle = '%zu%'; $array = explode(',', $array); $regex = like_to_regex($needle); $result = in_array_match($regex, $array); echo "haystack: "; print_r($array);echo "<br>"; echo "needle: ";print($needle)."<br>"; if($result == true){echo "result: true";}else{echo "result: false";}; //Example End function in_array_match($regex, $array) { if (!is_array($array)) trigger_error('Argument 2 must be array'); foreach ($array as $v) { $match = preg_match($regex, $v); if ($match === 1) { return true; }else{ return false; } } return false; } function like_to_regex($like, $casesensitive = FALSE, $escapechar = '\\') { $pattern = sprintf('~(?<!%1$s)(%1$s{2}|%%|_)~', preg_quote($escapechar)); $tokens = preg_split($pattern, $like, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach($tokens as &$token) { switch($token) { case $escapechar.$escapechar: $token = preg_quote($escapechar); break; case '_': $token = '.'; break; case '%': $token = '.*'; break; default: $token = preg_quote($token); } } return sprintf('~^%s$~%s', implode('', $tokens), $casesensitive ? '':'i'); } ---- Server IP: 69.147.83.199 Probable Submitter: 82.127.223.215 ---- Manual Page -- http://www.php.net/manual/en/function.in-array.php Edit -- https://master.php.net/note/edit/110637 Del: integrated -- https://master.php.net/note/delete/110637/integrated Del: useless -- https://master.php.net/note/delete/110637/useless Del: bad code -- https://master.php.net/note/delete/110637/bad+code Del: spam -- https://master.php.net/note/delete/110637/spam Del: non-english -- https://master.php.net/note/delete/110637/non-english Del: in docs -- https://master.php.net/note/delete/110637/in+docs Del: other reasons-- https://master.php.net/note/delete/110637 Reject -- https://master.php.net/note/reject/110637 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#191999) next »