php 验证,如果字符串以给定的子字符串开头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3254222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:08:30  来源:igfitidea点击:

verify, if the string starts with given substring

php

提问by Simon

i have a string in $strvariable.

我有一个$str变量字符串。

how can i verify is it starts with some word?

我如何验证它是否以某个词开头?



example

例子

$str = "http://somesite.com/somefolder/somefile.php";

when i wrote the following script returns yes

当我编写以下脚本时返回 yes

if(strpos($str, "http://") == '0') echo "yes";

BUT it returns yeseven when i wrote

但是即使我写了它也会返回yes

if(strpos($str, "other word here") == '0') echo "yes";

i think it strposreturns zeroif it can't find substring too(or empty value).

我认为如果它也找不到子字符串(或空值),它就会strpos返回zero

so, what can i do, if i want to verify the word, which in the start of string?(maybe i must use ===in this case?)

那么,如果我想验证字符串开头的单词,我该怎么办?(也许===在这种情况下我必须使用?)

回答by cletus

You need to do:

你需要做:

if (strpos($str, "http://") === 0) echo "yes"

The ===operator is a strict comparison that doesn't coerce types. If you use ==then false, an empty string, null, 0, an empty array and a few other things will be equivalent.

===运营商进行严格比较并不强制类型。如果使用==then false,则空字符串、null0、空数组和其他一些东西将是等效的。

See Type Juggling.

请参阅类型杂耍

回答by Dennis Haarbrink

You should check with the identity operator(===), see the documentation.

您应该检查身份运算符(===),请参阅文档

Your test becomes:

你的测试变成:

if(strpos($str, "http://") === 0) echo "yes"; //returns yes

回答by Ray

PHP does have 2 functions to verify if a string starts with a given substring:

PHP 确实有 2 个函数来验证字符串是否以给定的子字符串开头:

  • strncmp(case sensitive);
  • strncasecmp(case insensitive);
  • strncmp(区分大小写);
  • strncasecmp(不区分大小写);

So if you want to test only http (and not https), you can use:

因此,如果您只想测试 http(而不是 https),则可以使用:

 if (strncasecmp($str,'http://',7) == 0) echo "we have a winner"

回答by Raja

check with

检查

if(strpos($str, "http://") === 0) echo "yes";

as ==will turn positive for both false & 0 check the documentation

==将转均为阳性假0检查文档

回答by SimonW

Another option is:

另一种选择是:

if (preg_match("|^(https?:)?\/\/|i", $str)) {
    echo "the url starts with http or https upper or lower case or just //.";
}

As shown here: http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/

如图所示:http: //net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/

回答by Luca C.

strncmp($str, $word, strlen($word))===0

Is a bit more performant than strpos

性能比 strpos 高一点

回答by Ben Everard

if(substr($str, 0, 7)=="http://") {
    echo("Statrs with http://");
}

回答by deceze

There's a big red warning in the documentationabout this:

文档中有一个很大的红色警告:

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

此函数可能返回布尔值 FALSE,但也可能返回计算结果为 FALSE 的非布尔值,例如 0 或“”。请阅读有关布尔值的部分以获取更多信息。使用 === 运算符来测试此函数的返回值。

strposmay return 0or false. 0is equal to false(0 == false). It is not identicalto false however, which you can test with 0 === false. So the correct test is if (strpos(...) === 0).

strpos可能返回0false0等于false0 == false)。这是不相同的,以假然而,你可以测试0 === false。所以正确的测试是if (strpos(...) === 0).

Be sure to read up on the difference, it's important: http://php.net/manual/en/language.operators.comparison.php

请务必阅读差异,这很重要:http: //php.net/manual/en/language.operators.comparison.php