判断一个 PHP 字符串是否以另一个字符串结尾的最有效测试是什么?

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

What's the most efficient test of whether a PHP string ends with another string?

phpstringperformance

提问by Jason Cohen

The standard PHP way to test whether a string $strends with a substring $testis:

测试字符串是否$str以子字符串结尾的标准 PHP 方法$test是:

$endsWith = substr( $str, -strlen( $test ) ) == $test

Is this the fastest way?

这是最快的方法吗?

回答by mcrumley

What Assaf said is correct. There is a built in function in PHP to do exactly that.

阿萨夫说的是对的。PHP 中有一个内置函数可以做到这一点。

substr_compare($str, $test, strlen($str)-strlen($test), strlen($test)) === 0;

If $testis longer than $strPHP will give a warning, so you need to check for that first.

如果$test超过$strPHP 会给出警告,所以你需要先检查一下。

function endswith($string, $test) {
    $strlen = strlen($string);
    $testlen = strlen($test);
    if ($testlen > $strlen) return false;
    return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
}

回答by jscheel

This method is a tiny bit more memory-expensive, but it is faster:

这种方法会占用更多内存,但速度更快:

stripos(strrev($haystack), $reversed_needle) === 0;

This is best when you know exactly what the needle is, so you can hard-code it reversed. If you reverse the needle programatically, it becomes slower than the earlier method.

当您确切地知道针是什么时这是最好的,因此您可以对其进行硬编码。如果您以编程方式反转针头,它会比之前的方法慢。

回答by Alexander Yanovets

$endsWith = substr_compare( $str, $test, -strlen( $test ) ) === 0

Negative offset "starts counting from the end of the string".

负偏移量“从字符串的末尾开始计数”。

回答by Patrick Smith

Here's a simple way to check whether one string ends with another, by giving strposan offset right where the string should be found:

这是一种检查一个字符串是否以另一个字符串结尾的简单方法,方法是strpos在应该找到字符串的位置提供一个偏移量:

function stringEndsWith($whole, $end)
{
    return (strpos($whole, $end, strlen($whole) - strlen($end)) !== false);
}

Straightforward, and I think this'll work in PHP 4.

直截了当,我认为这将适用于 PHP 4。

回答by Assaf Lavie

It depends on which sort of efficiency you care about.

这取决于您关心哪种效率。

Your version uses more memory due to the extra copy from the use of substr.

由于使用 substr 的额外副本,您的版本使用了更多内存。

An alternative version might search the original string for the last occurrence of the substring without making a copy, but would probably be slower due to more testing.

替代版本可能会在不复制的情况下搜索原始字符串中最后一次出现的子字符串,但由于更多测试,可能会更慢。

Probably the most efficient way is to do loop char-by-char from the -sterlen(test) position till the end of the string and compare. That's the minimal amount of comparisons you can hope to do and there's hardly any extra memory used.

可能最有效的方法是从 -sterlen(test) 位置到字符串末尾逐个字符地循环并进行比较。这是您希望进行的最少量比较,并且几乎不使用任何额外内存。

回答by Gumbo

Another way would be to use the strrposfunction:

另一种方法是使用该strrpos函数

strrpos($str, $test) == strlen($str) - strlen($test)

But that's not faster.

但这并没有更快。

回答by Srinivasan.S

I hope that the below answer may be efficient and also simple:

我希望以下答案既有效又简单:

$content = "The main string to search";
$search = "search";
//For compare the begining string with case insensitive. 
if(stripos($content, $search) === 0) echo 'Yes';
else echo 'No';

//For compare the begining string with case sensitive. 
if(strpos($content, $search) === 0) echo 'Yes';
else echo 'No';

//For compare the ending string with case insensitive. 
if(stripos(strrev($content), strrev($search)) === 0) echo 'Yes';
else echo 'No';

//For compare the ending string with case sensitive. 
if(strpos(strrev($content), strrev($search)) === 0) echo 'Yes';
else echo 'No';

回答by wloske

Don't know if this is fast or not but for a single character test, these work, too:

不知道这是否很快,但对于单个字符测试,这些也有效:

(array_pop(str_split($string)) === $test) ? true : false;
($string[strlen($string)-1] === $test) ? true : false;
(strrev($string)[0] === $test) ? true : false;

回答by Biskrem Muhammad

easiest way to check it via regular expression

通过正则表达式检查它的最简单方法

for exampleto check if the mail given is gmail:

例如检查给定的邮件是否是 gmail:

echo (preg_match("/@gmail\.com$/","[email protected]"))?'true':'false';

回答by kabantejay

for single-char needle:

对于单碳针:

if (@strrev($haystack)[0] == $needle) {
   // yes, it ends...
}