iam using preg_match as follows:
case 1: pattern has two words. with the preg-match solution - it return two results.
case 2: pattern has three words. with the preg-match solution - it return two results.
in my opinion case 2 will return only one result. but i havent any approach.
i tryed with negation - so is the pattern
$pattern="^(^Jim|^goes|^with)"; )
or
$pattern="(Jim|goes|with){1}"
goes wrong or
$pattern="(Jim){1}(with){1}"
goes wrong
for explain:
$pattern1="(Jim|goes|with)";
$_search[0]="Jim Carrey goes crazy";
$_search[1]="Jim Carrey goes crazy with santa clause";
preg_match("/$pattern1/is",$_search[0] )
preg_match("/$pattern1/is",$_search[1] )
is it possible to get an and as pattern for one result in my example ?
THANKS - i hope it
Edit: Input(i) - Output(o) Examples(e)
e1 i: (Jim){1}(goes){1}(with){1}
e1 o: no result
e2 i: (Jim|goes|with)
e2 o: two matches "Jim Carrey goes crazy" and "Jim Carrey goes crazy with santa clause"
e3 i: ^(^Jim|^goes|^with)
e3 o: two matches "Jim Carrey goes crazy" and "Jim Carrey goes crazy with santa clause"
which imput solution comes with one result ?
which solution with input: "Jim goes with" generate one result for example:"Jim Carrey goes crazy with santa clause" it means an and condition in regex - but is it possible ?
SOLUTION:
$patternsearch=chop("Jim goes with ");
if(preg_match('/ /',$patternsearch)){
$_array_pattern = explode( ' ', $patternsearch );
$text = preg_replace('/ /','|',$patternsearch);
$pattern1=''.'('.$text.')';
}else
{
$pattern1 = $patternsearch;
}
echo "my search is as follow: $patternsearch"."</br>";
echo "my pattern is as follow: $pattern1"."</br></br>";
foreach($_search as $search){
$andcounter= preg_match_all("/$pattern1/isx", $search,$matscha);
echo "preg_match count: $andcounter =";
echo "search count : ".count($_array_pattern)."</br></br>";
if(count($_array_pattern) === $andcounter ){
$item[]=$search;
}
}
echo "<pre>";
var_dump($item);
echo "</pre>" ;
OUTPUT:
my search is as follow: Jim goes with
my pattern is as follow: (Jim|goes|with)
preg_match count: 2 =search count : 3
preg_match count: 3 =search count : 3
array(1) {
[0] =>
string(39) "Jim Carrey goes crazy with santa clause"
}
and with:
$patternsearch="Jim goes ";
my search is as follow: Jim goes
my pattern is as follow: (Jim|goes)
preg_match count: 2 =search count : 2
preg_match count: 2 =search count : 2
array(2) {
[0] =>
string(21) "Jim Carrey goes crazy"
[1] =>
string(39) "Jim Carrey goes crazy with santa clause"
}
Hei,
What about using preg_match_all (http://php.net/manual/en/function.preg-match-all.php) with $pattern="(Jim|goes|with)";