我想检查给定的字符串是否是另一个字符串的子字符串。例如:检查单词是否是子字符串 - php
$a = 'Hello World!';
$b = 'ell';
$ b是$ a的子字符串。任何可以检查这个功能的功能?
我想检查给定的字符串是否是另一个字符串的子字符串。例如:检查单词是否是子字符串 - php
$a = 'Hello World!';
$b = 'ell';
$ b是$ a的子字符串。任何可以检查这个功能的功能?
试试这个
$a = 'Hello World!';
$b = 'ell';
if (strpos($a, $b) !== false) {
echo true; // In string
}
可以使用strpos()
函数来检查
<?php
if (strpos('Hello World!','ell') !== false) {
echo 'contains';
}
else echo 'not contains';
$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";
}
你有没有尝试谷歌吗? – Vucko 2014-09-11 11:07:43
您是否考虑过使用Google搜索您的问题? – 2014-09-11 11:07:48
我发现的唯一的东西是substr,但它的工作原理就像(“abcdef”,-1);对?它不比较2个字符串? – Chris 2014-09-11 11:09:21