Reputation:
Lets suppose I have String as
$b = 'grapes, pineapple & fruit seller offered by flipkart from usa';
From this string I want 3 words as follow
The first word I get by explode()
Third word I get as follow Code
$bb = explode(",",$b);
$demo1 = substr($bb[1], strpos($bb[1], "&") + 1)."<br>";
$demo2 = str_replace($demo1, "", $bb[1])."<br>";
$result = substr($demo2, strpos($demo2, "&") + 1);
$newres = explode(" ",$result);
foreach($newres as $value){
if($value == "offered"){
break;
}
$myarr[] = $value;
}
$thirdkeyword = implode(" ",$myarr);
echo "Third Keyword :".$thirdkeyword."<br>";
Which returns me fruit seller
.
But now I want second word pineapple. So how can I get the word before &(AND).
Upvotes: 1
Views: 24
Reputation: 26153
$b = 'grapes, pineapple & fruit seller offered by flipkart from usa';
$str = explode(' offered', $b)[0];
$result = preg_split('/\s*[,&]\s*/', $str);
print_r($result);
result
Array
(
[0] => grapes
[1] => pineapple
[2] => fruit seller
)
Upvotes: 2