开发者

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

开发者 https://www.devze.com 2023-03-23 01:15 出处:网络
This question already has answers here: Closed 11 years ago. 开发者_C百科 Possible Duplicate: See if one string contains another string
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

精彩评论

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