note 84910 deleted from function.strpos by levim
| From: | levim@php.net | Date: | Tue, 22 Jul 2014 03:19:42 +0000 |
| Subject: | note 84910 deleted from function.strpos by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-200007@lists.php.net to get a copy of this message | ||
Note Submitter: Chuzasoft Inc. ARGENTINA
----
Hi! Don't you people miss the pretty comparison operator 'LIKE' from mySql in PHP??.
I've made this funtion to emulate that method. It's for search a match string into another
String
using the '%' caracter just like you do un the LIKE syntax.
For example:
<?php
$mystring = "Hi, this is good!";
$searchthis = "%thi% goo%";
$resp = milike($mystring,$searchthis);
if ($resp){
echo "milike = VERDADERO";
} else{
echo "milike = FALSO";
}
?>
Will print:
milike = VERDADERO
and so on...
this is the function:
<?php
function milike($cadena,$busca){
if($busca=="") return 1;
$vi = split("%",$busca);
$offset=0;
for($n=0;$n<count($vi);$n++){
if($vi[$n]== ""){
if($vi[0]== ""){
$tieneini = 1;
}
} else {
$newoff=strpos($cadena,$vi[$n],$offset);
if($newoff!==false){
if(!$tieneini){
if($offset!=$newoff){
return false;
}
}
if($n==count($vi)-1){
if($vi[$n] != substr($cadena,strlen($cadena)-strlen($vi[$n]), strlen($vi[$n]))){
return false;
}
} else {
$offset = $newoff + strlen($vi[$n]);
}
} else {
return false;
}
}
}
return true;
}
?>
Good luck!