【发布时间】:2014-08-12 12:45:51
【问题描述】:
现在我使用stristr($q, $string) 但如果
$string = "one monkey can jump 66 times";
$q = "monkey 66";
我想知道这个字符串是否同时包含monkey 和66。
我该怎么做?
【问题讨论】:
-
你想在两个字符串中搜索相似的词吗??
现在我使用stristr($q, $string) 但如果
$string = "one monkey can jump 66 times";
$q = "monkey 66";
我想知道这个字符串是否同时包含monkey 和66。
我该怎么做?
【问题讨论】:
正如post 中所报告的,第二种方法速度更快,内存占用更少。
好吧,看看这几行:
// here there are your string and your keywords
$string = "one monkey can jump 66 times";
$q = "monkey 66";
// initializate an array from keywords in $q
$q = explode(" ", $q);
// for every keyword you entered
foreach($q as $value) {
// if strpos finds the value on the string and return true
if (strpos($string, $value))
// add the found value to a new array
$found[] = $value;
}
// if all the values are found and therefore added to the array,
// the new array should match the same object of the values array
if ($found === $q) {
// let's go through your path, super-man!
echo "ok, all q values are in string var, you can continue...";
}
【讨论】:
if(stristr('monkey', $string) && stristr('66', $string)) {
//Do stuff
}
【讨论】:
通过给他们一个变量 $monkey,$value ($monkey jumps $value) 来简单地发布你的变量值,然后获取它的值
【讨论】:
您可以使用strpos() 函数来查找一个字符串在另一个字符串中的出现:
$a = 'How are you?';
if (strpos($a, 'are') !== false) {
echo 'true';
}
请注意,使用!== false 是故意的(!= false 和=== true 都不起作用); strpos() 返回针字符串在 haystack 字符串中开始的偏移量,如果未找到针,则返回布尔值 false。由于 0 是一个有效的偏移量,而 0 是“错误的”,我们不能使用像 !strpos($a, 'are') 这样更简单的构造。
【讨论】: