,我們有一個像字符串:如何檢查某些字符是否在字符串中?
$string="Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
所以我要檢查,如果有的話是在該字符串可用。例如詞:
$words=['Ipsum','54'];
,你可以看到Ipsum
是字符串中,但54
不是字符串,因此該函數應該再打true
(因爲Ipsum
找到)。 我使用PHP,感謝您的幫助。
,我們有一個像字符串:如何檢查某些字符是否在字符串中?
$string="Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
所以我要檢查,如果有的話是在該字符串可用。例如詞:
$words=['Ipsum','54'];
,你可以看到Ipsum
是字符串中,但54
不是字符串,因此該函數應該再打true
(因爲Ipsum
找到)。 我使用PHP,感謝您的幫助。
php > $string="Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
php > $lookfor = "simply";
php > echo strpos($string, $lookfor) > 0 ? true : false ;
1
php > $lookfor = "simplest";
php > echo strpos($string, $lookfor) > 0 ? true : false ;
//nothing will be printed -))
只是讓從上面的代碼中的函數。
更新
$ 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
'strpos'也可以返回0,如果搜索詞在位置'0' - 你應該檢查'false'而不是'> 0' – Philipp
是的,這是正確的 - )) – marmeladze
感謝您的幫助,但看到這個代碼:ali98.xyz/filter.txt,它沒有工作! –
一個簡單的循環與strpos
結合應該是所有你需要做到這一點
function containsOneOfThoseWords($str, $words) {
foreach ($words as $word) {
if (strpos($str, $word) !== false) return true;
}
return false;
}
感謝您的幫助,但看到下面的代碼:https://ali98.xyz/filter.txt,它沒有工作! –
@AliYQB你應該把參數傳遞給函數......'containsOneOfThoseWords($ str,$ words)' – Philipp
***'$字=陣列( '存有', '54');' –
自己創建一個函數,並把strpos它 –
您應該使用strpos,運行的foreach ()for $ words數組,並在foreach中使用strpos。 –