PHP查詢字串中是否包含多個指定字串

2022-09-24 04:03:11 字數 755 閱讀 5915

我有乙個字串,需要檢查幾個字元。我可以用strpos();做到這一點。但是在這種情況下,我將需要多次使用strpos();。像這樣的東西:

if(strpos($str, "-") === false &&

strpos($str, "_") === false &&

strpos($str, "@") === false &&

strpos($str, "/") === false &&

strpos($str, "'") === false &&

strpos($str, "]") === false &&

strpos($str, "[") === false &&

顯然是不行的,於是找到了這個方法

//被查詢的字串

$string = '中文,string,21565489fewwords';

//指定的字串

$arr = ['中文','string', 'few', 'words'];

preg_match_all('#('.implode('|', $arr).')#', $string, $wordsfound);

//獲取匹配到的字串,array_unique()函式去重。如需獲取總共出現次數,則不需要去重

$wordsfound = array_unique($wordsfound[0]);

if(count($wordsfound) >= 2)else

完美。。。。。。。。。。

查詢字串中是否包含相同字元

題目 請實現乙個演算法,確定乙個字串的所有字元是否全都不同。這裡我們要求不允許使用額外的儲存結構。給定乙個string,inistring,請返回乙個bool值 true代表所有字元全都不同,false代表存在相同的字元。保證字串中的字元為ascii字元。字串的長度小於等於3000。測試樣例 aei...

查詢字串 php

php strpos 函式 strpos 函式查詢字串在另一字串中第一次出現的位置。注釋 strpos 函式對大小寫敏感。注釋 該函式是二進位制安全的。strpos string,find,start 引數 描述string 必需。規定要搜尋的字串。find 必需。規定要查詢的字串。start 可選...

Linux 在多個檔案中查詢字串

線上執行環境中,日誌檔案按天分類,有時候需要從這些日誌檔案中查詢某些字串,並且獲取字串所在日誌檔案,來進行錯誤排查 例子一檔案少的時候,直接用grep即可 輸入 grep 8090 ttt.conf test.conf test2.conf 輸出 ttt.conf listen 8090 test2...