2011-12-16 75 views

回答

1

PHP strstr

<?php 
    $x = 'cug hs ib'; 
    $y = strstr($x, 'hs'); 
    echo $y; 
?> 

更新: 更好的用戶strpos

<?php 
    $x = 'cug hs ib'; 
    $y = strpos($x, 'hs'); 
    if($y === false) 
     // Not a substring 
    else 
     // Substring 

?> 
+1

是什麼+的strstr之間strpos的區別? – 2011-12-16 22:46:40

+0

strstr返回從第一次出現開始的字符串,而strpos返回子串的數字(位置) – SlavaNov 2011-12-16 22:48:22

+0

@JosephTorraca:`strstr`查找字符串中第一次出現子字符串,從該索引返回字符串,除非第三個參數是設置爲「true」。 `strpos`返回第一次出現子字符串的第一個字符的索引,如果沒有找到則返回false。 – 2011-12-16 22:49:56

1
if(strpos($big_string, $sub_string) !== false) 
{ 
    // $big_string contains $sub_string 
} 
1

你可以使用strpos

if (strpos($haystack, $needle) !== false) { 
    echo "the string '{$needle}' was found within '{$haystack}'"; 
}