php如何判斷乙個字串是否包含另乙個字串

2021-10-01 14:39:41 字數 1120 閱讀 7797

來自1:

我覺得最簡單的就是:(用這種最好,strpos效率最高)

strpos($a, $b) !== false 如果$a 中存在 $b,則為 true ,否則為 false。

用 !== false (或者 === false) 的原因是如果 $b 正好位於$a的開始部分,那麼該函式會返回int(0),那麼0是false,但$b確實位於$a中,所以要用 !== 字串是否包含另乙個字串.html' target='_self'>判斷一下型別,要確保是嚴格的 false。

其它的還有 php 原生支援的函式,如 strstr(),stristr() 等,直接判斷就可以了。

來自2:

php語言是乙個功能強大的嵌入式html指令碼語言,它的易用性讓許多程式設計師選擇使用。php判斷字串的包含,可以使用php的內建函式strstr,strpos,stristr直接進行判斷.也可以通過explode函式的作用寫乙個判斷函式。

1. strstr: 返回乙個從被判斷字元開始到結束的字串,如果沒有返回值,則不包含

**如下:

< ?php   

/*如手冊上的舉例*/   

$email = '[email protected]';   

$domain = strstr($email, '@');   

echo $domain; 

// prints @example.com   

?> 

2. stristr: 它和strstr的使用方法完全一樣.唯一的區別是stristr不區分大小寫.

3. strpos: 返回boolean值.false和true不用多說.用 「===」進行判斷.strpos在執行速度上都比以上兩個函式快,另外strpos有乙個引數指定判斷的位置,但是預設為空.意思是判斷整個字串.缺點是對中文的支援不好. 

php判斷字串的包含**如下:

$str= 'abc';   

$needle= 'a';   

$pos = strpos($str, $needle);  

4. 用explode進行判斷 

php判斷字串的包含**如下:

function checkstr($str) else 

SQL 判斷乙個字串是否在另外乙個字串中

eg str1 admin str2 1234,123admin,xcxx 比較str1是否在str2中 用常用的charindex,返回肯定是有值的,這裡自己動手寫乙個方法 檢查乙個字串是否在另外乙個字串中數,另外乙個字串元素用,隔開 create function dbo checkstrina...

php判斷乙個字串包含另乙個字串

a 58252,58253 如果 a 中存在 b,則為 true 否則為 false。b 58253 if strpos a,b false else 查詢字串在陣列中出現的次數 array array 1,hello 1,world hello 11 計算 string在 array 需為陣列 中...

判斷乙個字串是否在另乙個字串中

方法一 string str1 nihaoksdoksad string str2 ok int total 0 for string tmp str1 tmp null tmp.length str2.length tmp tmp.substring 1 system.out.println st...