Php 如何检查字符串中是否包含某些字符?
我们有这样一个字符串:Php 如何检查字符串中是否包含某些字符?,php,Php,我们有这样一个字符串: $string="Lorem Ipsum is simply dummy text of the printing and typesetting industry."; 所以我想检查这个字符串中是否有可用的单词。例如: $words=['Ipsum','54']; 您可以看到Ipsum在字符串中,但54不在字符串中,因此函数应该回调true(因为Ipsum已找到)。 我正在使用php,谢谢你的帮助。一个简单的循环与strpos相结合就可以实现这一点 function
$string="Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
所以我想检查这个字符串中是否有可用的单词。例如:
$words=['Ipsum','54'];
您可以看到Ipsum
在字符串中,但54
不在字符串中,因此函数应该回调true
(因为Ipsum
已找到)。
我正在使用php,谢谢你的帮助。一个简单的循环与
strpos
相结合就可以实现这一点
function containsOneOfThoseWords($str, $words) {
foreach ($words as $word) {
if (strpos($str, $word) !== false) return true;
}
return false;
}
只需从上面的代码片段中创建一个函数
更新
$ cat search.php
<?php
$domain="asdasd asd fasdfasfafafad f454 asdfa";
$lookfor=array('as','f454');
function containsOneOfThoseWords($str, $words) {
foreach ($words as $word) {
if (strpos($str, $word) !== false) return true;
}
return false;
}
echo containsOneOfThoseWords($domain, $lookfor);
?>
$ php search.php; echo
1
$cat search.php
$php search.php;回音
1.
***$words=array('Ipsum','54')代码>自己创建一个函数并将strpos放入其中您应该使用strpos,为$words数组运行foreach(),并在foreach中使用strpos。请查看侧栏中的问题。strpos
也可以返回0
,这很好,如果搜索词位于位置0
-您应该检查false
,而不是>0
是的,没错-)谢谢您的帮助,但看到以下代码:ali98.xyz/filter.txt,它不起作用!您应该将参数传递给containsOneOfTheseWords
函数。我已修复它-请查看我的更新答案。感谢您的帮助,但看到以下代码:,它不起作用@AliYQB您应该将参数传递给函数<代码>包含一个单词($str,$words)
$ cat search.php
<?php
$domain="asdasd asd fasdfasfafafad f454 asdfa";
$lookfor=array('as','f454');
function containsOneOfThoseWords($str, $words) {
foreach ($words as $word) {
if (strpos($str, $word) !== false) return true;
}
return false;
}
echo containsOneOfThoseWords($domain, $lookfor);
?>
$ php search.php; echo
1