我想搜索包含Rathi 25mm
的字符串,但我不想使用完整的单词进行搜索。如何使用特定字词进行搜索?
Rathi Stelmax 500 25 mm in stripos
<?php
$title='Rathi Stelmax 500 25 mm';
if (stripos(strtolower($title), 'Rathi 25 mm') !== false)
{
echo 'true';
}
?>
答案 0 :(得分:0)
您可以尝试使用此脚本
$title="Rathi Stelmax 500 25 mm";
if(preg_match("/(Rathi|25|mm)/i", $title)){
//one of these string found
}
答案 1 :(得分:0)
最好的方法是使用preg_match_all。
这是一个REGEX功能。 这是一个小例子:
$input_lines = 'Hello i am 250 years old from Mars'
preg_match_all("/(\d+)|(Mars)/", $input_lines, $output_array);
$ output_array将包含一个包含以下数据的数组:
0 => array(2
0 => 250
1 => Mars
)
答案 2 :(得分:0)
如果您不愿意使用正则表达式,则可以将单词中的搜索字符串拆分,并检查每个字符串是否包含在字符串中:
$title = 'Rathi Stelmax 500 25 mm';
$lookFor = 'Rathi 25 mm';
$counter = 0;
$searchElements = explode(' ', $lookFor);
foreach($searchElements as $search){
if(strpos($title, $search) !== false){
$counter++;
}
}
if(count($searchElements) == $counter){
echo 'found everything';
}
else{
echo 'found ' . $counter . ' element(s)';
}
它可能不如正则表达式有效,但也可能更容易掌握。
答案 3 :(得分:0)
有几种方法可以做到这一点。使用您当前的方法,您可以在条件中运行多个stripos
以确认每个单词是否存在:
$title='Rathi Stelmax 500 25 mm';
if (stripos(strtolower($title), 'Rathi') !== false && stripos(strtolower($title), '25 mm'))
{ echo 'true';
}
您还可以使用正则表达式,例如:
/Rathi.*25 mm/
PHP演示:https://eval.in/628148
正则表达式演示:https://regex101.com/r/cZ6bL1/1
PHP用法:
$title='Rathi Stelmax 500 25 mm';
if (preg_match('/Rathi.*25 mm/', $title)) {
echo 'true';
}