检查word是否是子字符串 - php

时间:2014-09-11 11:05:42

标签: php

我想检查给定字符串是否是另一个字符串的子字符串。例如:

$a = 'Hello World!';
$b = 'ell';

$ b是$ a的子字符串。有什么功能可以检查吗?

3 个答案:

答案 0 :(得分:1)

您可以使用strpos()功能检查

    <?php
        if (strpos('Hello World!','ell') !== false) {
        echo 'contains';
    }
   else echo 'not contains';

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

答案 1 :(得分:0)

试试这个

$a = 'Hello World!';
$b = 'ell';
if (strpos($a, $b) !== false) {
    echo true; // In string
}

答案 2 :(得分:0)

$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

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";
}