删除字符串的一部分,但仅当它位于字符串末尾时才删除
我需要删除字符串的子字符串,但仅当它位于字符串的末尾时。
例如,删除以下字符串末尾的“字符串”:
"this is a test string" -> "this is a test "
"this string is a test string" - > "this string is a test "
"this string is a test" -> "this string is a test"
有什么想法吗?可能是某种 preg_replace,但是如何?
I need to remove a substring of a string, but only when it is at the END of the string.
for example, removing 'string' at the end of the following strings :
"this is a test string" -> "this is a test "
"this string is a test string" - > "this string is a test "
"this string is a test" -> "this string is a test"
Any idea's ? Probably some kind of preg_replace, but how??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我想你可以使用 正则表达式,它将匹配
字符串
,然后字符串结尾,再加上preg_replace()
函数。Something like this should work just fine :
Notes :
string
匹配...好吧...string
$
表示 字符串结尾有关更多信息,您可以阅读模式语法 PHP 手册的部分。
I suppose you could use a regular expression, which would match
string
and, then, end of string, coupled with thepreg_replace()
function.Something like this should work just fine :
Notes :
string
matches... well...string
$
means end of stringFor more informations, you can read the Pattern Syntax section of the PHP manual.
preg_replace 和此模式: /string\z/i
\z 表示字符串结尾
http://tr.php.net /preg_replace
preg_replace and this pattern : /string\z/i
\z means end of the string
http://tr.php.net/preg_replace
PHP 8 版本
PHP 8 version
如果您不介意性能并且字符串的一部分只能放置在字符串的末尾,那么您可以这样做:
IF you don't mind about performance AND the part of the string could be placed only at the end of string, THEN you can do this:
@Skrol29的答案是最好的,但这里它是函数形式并使用三元运算符:
@Skrol29's answer is the best, but here it is in function form and using the ternary operator:
使用 PHP 8,代码可以更简单。
With PHP 8, the code can be simpler.
有多种技术可用于将一根针线从另一根线的末端移除。 即使在多字节字符串上,下面的所有代码片段也能正确执行。某些函数具有细微的行为,我们将对此进行描述。
使用 preg_replace() 将无缝地允许替换干草堆字符串数组,这可能不需要手动实现循环。
preg_replace()
可以提供更多关于不区分大小写、全字匹配的工具,并可能在匹配之前清除不需要的空格或标点符号(如果需要)。将动态字符串值传递给preg_
函数时,建议对字符串中对正则表达式引擎具有特殊含义的字符进行转义。代码: (PHPize 演示)
在图案中的针周围使用引号标记。这可以节省
preg_quote()
调用,但如果针包含\E
,则可能会被破坏。使用
preg_quote()
将是转义针字符串中字符的最可靠方法。这可能是我专业使用的方法,因为它不需要考虑条件。由于模式分隔符是#
,因此preg_quote()
不需要第二个参数 - 如果模式分隔符是正斜杠,则情况并非如此。有条件地使用
substr_replace()
删除不需要的后缀。有条件地使用
substr()
来隔离针之前的前导字符。有条件地使用
mb_substr()
和mb_strlen()
来隔离针前的前导字符。请注意,非
mb_
函数的行为与mb_
函数相同,因为substr_replace()
和substr( )
两者都对字节进行操作,而不是字符。由于mb_substr()
的参数是字符,因此长度参数中必须使用mb_strlen()
。There are multiple techniques which can be used to remove a needle string from the end of another string. All snippets below will perform correctly even on multibyte strings. Some functions have nuanced behaviors which will be described.
Using
preg_replace()
will seamlessly allow replacements on an array of haystack strings which may spare the need to manually implement a loop.preg_replace()
can offer more tooling around case-insensitivity, whole-word matching, and potentially cleaning up unwanted spaces or punctuation before the match (if desired). When passing a dynamic string value to apreg_
function, it is recommended to escape characters in the string with special meaning to the regex engine.Codes: (PHPize Demo)
Use quoting markers around the needle in the pattern. This saves a
preg_quote()
call, but could be broken if the needle contains\E
.Using
preg_quote()
will be the most reliable way to escape characters in the needle string. This is probably the method that I would use professionally because it doesn't need to faff around with conditions. Because the pattern delimiter is#
,preg_quote()
doesn't need a second parameter -- this is not true if the pattern delimiters are forward slashes.Conditionally use
substr_replace()
to remove the unwanted suffix.Conditionally use
substr()
to isolate the leading characters before the needle.Conditionally use
mb_substr()
andmb_strlen()
to isolate the leading characters before the needle.Notice that the non-
mb_
functions behave identically to themb_
function because the offset and length parameters ofsubstr_replace()
andsubstr()
both operate on bytes, not characters. Becausemb_substr()
's parameters are characters,mb_strlen()
must be used in the length parameter.您会注意到
$
字符的使用,它表示字符串的结尾:如果字符串是用户提供的变量,则最好通过
preg_quote
:You'll note the use of the
$
character, which denotes the end of a string:If the string is a user supplied variable, it is a good idea to run it through
preg_quote
first:如果子字符串包含特殊字符,则使用正则表达式可能会失败。
以下内容适用于任何字符串,并遵循内置字符串函数使用的约定:
Using regexp may fails if the substring has special characters.
The following will work with any strings and follows conventions used by built-in string functions:
我为字符串的左右修剪编写了这两个函数:
I wrote these two function for left and right trim of a string: