|
Posted by Rik on 08/11/06 23:42
simon wrote:
> Hi,
>
> I need to a regex to look for a string, (case insensitive), but
> excluding spaces and returns
>
> So that something like,
>
> $mystring1 = "The House";
> $mystring2 = "The Ho\r\nuse";
> $mystring3 = "The Ho u\n se";
Normally I'd say: "Why not remove all whitespace characters and then
search?"
> and the needle was "House" and all three string would return the
> start and end point.
You mean the index of the start end the end? Oh boy. You do realize BTW that
\n is a single character here?
> Can anybody help me with that?
Well, it isn't very pretty but, but I had a lot of fun (PHP >5, due to
str_split, it's possible to define your own str_split beforehand, check the
user comments on www.php.net):
function some_function($needle,$string,$all=false,$case=true){
$chars = str_split($needle,1);
$del = array_fill(0,count($chars),'/');
$pattern = '/'.implode('\s*?',array_map('preg_quote',$chars,$del)).'/s';
if($case) $pattern .= 'i';
echo $pattern;
if($all){
preg_match_all($pattern,$string,$matches,PREG_SET_ORDER+PREG_OFFSET_CAPTURE)
;
if(empty($matches)) return false;
$return = array();
foreach($matches as $key => $match){
$return[$key]['start'] = $match[0][1];
$return[$key]['end'] = $match[0][1] + strlen($match[0][0]);
}
return $return;
} else {
preg_match($pattern,$string,$matches,PREG_OFFSET_CAPTURE);
if(empty($matches)) return false;
return array('start' => $matches[0][1],'end' => $matches[0][1] +
strlen($matches[0][0]));
}
}
print_r(some_function("house","ho \t u\n\rse\n hoU se \n\n
house",true,true));
usage: some_function($needle,$string,$all,$case):
$needle: the text to search for.
$string: the string to search in.
$all: wether to return only the first match (false, default), or all matches
$case: wether the search is case-sensitive (default) or not.
This will ignore ALL whitespace characters, so also tabs besides spaces and
returns.
Grtz,
--
Rik Wasmus
Navigation:
[Reply to this message]
|