php判断字符串中是否包含某个字符串实现方法与示例

示例示例PHP中可以使用strpos函数来判断字符串中是否包含某个字符串,其原型为:int strpos string $ , mixed $needle [, int $offset = 0 ] )

PHP中可以使用strpos函数来判断字符串中是否包含某个字符串,其原型为:

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

该函数返回needle在haystack中首次出现的位置,如果没有找到则返回FALSE。

以下为示例代码:

$mystring = 'abc';

$findme = 'a';

$pos = strpos($mystring, $findme);

// Note our use of ===. Simply == would not work as expected

// because the position of 'a' was the 0th (first) character.

if ($pos === false) {

echo "The string '$findme' was not found in the string '$mystring'";

} else {

echo "The string '$findme' was found in the string '$mystring'";

echo " and exists at position $pos";

}

?>

上述代码的输出结果为:The string 'a' was found in the string 'abc' and exists at position 0

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(913)
string源码实现字符串操作的功能
上一篇
java字符串转list:从Java字符串转换为List的方法
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(59条)