php 如何检查字符串是否以指定的字符串开头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2790899/
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 07:41:12  来源:igfitidea点击:

How to check if a string starts with a specified string?

php

提问by Andrew

I'm trying to check if a string starts with http. How can I do this check?

我正在尝试检查字符串是否以http. 我怎样才能做这个检查?

$string1 = 'google.com';
$string2 = 'http://www.google.com';

回答by Kendall Hopkins

Use the substrfunction to return a part of a string.

使用substr函数返回字符串的一部分。

substr( $string_n, 0, 4 ) === "http"

If you're trying to make sure it's not another protocol. I'd use http://instead, since https would also match, and other things such as http-protocol.com.

如果您想确保它不是另一种协议。我会http://改用,因为 https 也会匹配,还有其他的东西,比如 http-protocol.com。

substr( $string_n, 0, 7 ) === "http://"

And in general:

一般来说:

substr($string, 0, strlen($query)) === $query

回答by awgy

Use strpos():

使用strpos()

if (strpos($string2, 'http') === 0) {
   // It starts with 'http'
}

Remember the three equals signs (===). It will not work properly if you only use two. This is because strpos()will return falseif the needle cannot be found in the haystack.

记住三个等号 ( ===)。如果您只使用两个,它将无法正常工作。这是因为如果在大海捞针中找不到针,strpos()它将返回false

回答by Sid

There is also the strncmp()function and strncasecmp()function which is perfect for this situation:

还有一个strncmp()功能和strncasecmp()功能非常适合这种情况:

if (strncmp($string_n, "http", 4) === 0)

In general:

一般来说:

if (strncmp($string_n, $prefix, strlen($prefix)) === 0)

The advantage over the substr()approach is that strncmp()just does what needs to be done, without creating a temporary string.

相对于这种substr()方法的优点是strncmp()只做需要做的事情,而不创建临时字符串。

回答by user276648

You can use a simple regex (updated version from user viriathusas eregiis deprecated)

您可以使用一个简单的regex(更新版本从用户viriathuseregi已废弃)

if (preg_match('#^http#', $url) === 1) {
    // Starts with http (case sensitive).
}

or if you want a case insensitive search

或者如果您想要不区分大小写的搜索

if (preg_match('#^http#i', $url) === 1) {
    // Starts with http (case insensitive).
}

Regexes allow to perform more complex tasks

正则表达式允许执行更复杂的任务

if (preg_match('#^https?://#i', $url) === 1) {
    // Starts with http:// or https:// (case insensitive).
}

Performance wise, you don't need to create a new string (unlike with substr) nor parse the whole string if it doesn't start with what you want. You will have a performance penalty though the 1st time you use the regex (you need to create/compile it).

在性能方面,您不需要创建新字符串(与 substr 不同),也不需要解析整个字符串,如果它不是以您想要的开头。尽管第一次使用正则表达式(您需要创建/编译它),但您会受到性能损失。

This extension maintains a global per-thread cache of compiled regular expressions (up to 4096). http://www.php.net/manual/en/intro.pcre.php

此扩展维护已编译正则表达式的全局每线程缓存(最多 4096 个)。 http://www.php.net/manual/en/intro.pcre.php

回答by Jake

You can check if your string starts with http or https using the small function below.

您可以使用下面的小函数检查您的字符串是否以 http 或 https 开头。

function has_prefix($string, $prefix) {
   return substr($string, 0, strlen($prefix)) == $prefix;
}

$url   = 'http://www.google.com';
echo 'the url ' . (has_prefix($url, 'http://')  ? 'does' : 'does not') . ' start with http://';
echo 'the url ' . (has_prefix($url, 'https://') ? 'does' : 'does not') . ' start with https://';

回答by viriathus

Also work:

还工作:

if (eregi("^http:", $url)) {
 echo "OK";
}