开发者

What is the best way to check whether string $a contains string $b [duplicate]

This question already has answers here: Closed 11 years ago. 开发者_C百科

Possible Duplicate:

See if one string contains another string

I have a string each word separated by ","

$a="apple,pear,peach";

$b='apple';

What is the best way to check whether string $a contains string $b


If $b will not contain , use strpos:

if (false !== strpos($a, $b)) {
  // $a contains $b
}

otherwise you can use:

if (in_array($b, explode(',', $a)) {
  // $a contains $b
}


See strstr or strpos


strpos($a, $b) !== false

Only way I know.


if (strpos($a, $b) !==false) { $a contains $b }

http://www.php.net/manual/en/function.strpos.php


Use regular expressions. http://php.net/manual/en/function.preg-match.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜