仅在匹配启动字符时返回值


stristr($haystack, $needle)

此功能将采用字符串$ haystack,浏览一下以找到字符串$针,如果发现将返回Haystack的全部值。

我只想在$ neader匹配任何数量的值以从开始时返回干草堆的值。

例如,Stristr将在所有示例中返回$ haystack,但我想要的是下面:

$haystack = "foo"
$needle =   "oo"
return false;
$haystack = "foo"
$needle =   "f"
return $haystack;
$haystack = "foo"
$needle =   "o"
return false;
$haystack = "foo"
$needle =   "fo"
return $haystack;

在我看来,这可能是PHP内置的东西,但我在文档中找不到任何东西。

谢谢。

您可以为此创建一个新功能,并在其中使用substrstrpos,例如:

function matchBegining($needle, $haystack)
{
    if(substr($haystack, 0, strlen($needle)) === $needle)
    {
        return $haystack;
    }
    return false;
}

如果匹配,则将返回$haystack,如果不是false

if语句 strpos

 if (strpos($haystack, $needle) === 0) {

最新更新