Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想使用 php 测试子字符串是否在字符串中。
例如,
$str="Cycle"; $str1="Cycle,Yoga";
我想检查一下if (str in str1)。
if (str in str1)
利用stripos
stripos
if (stripos($str1,$str) !== false) { echo 'true'; }
使用strpos(用于区分大小写的字符串)或stripos(用于不区分大小写)进行字符串匹配。
if (strpos($str1,$str) !== false) { // It'll check the case-sensitivity of string echo 'true'; } if (stripos($str1,$str) !== false) { // It'll ignore the case-sensitivity echo 'true'; }
使用 strpos 或 stripos
$mystring = 'Cycle,Yoga'; $findme = 'Cycle'; $pos = strpos($mystring, $findme); if ($pos !== false) { echo "true"; }