What is the best way to check whether string $a contains string $b [duplicate]
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
精彩评论