简体   繁体   English

PHP rtrim函数修剪多余的字符? 为什么?

[英]PHP rtrim function trim extra character? Why?

How does rtrim work? rtrim如何工作? I have a string "4dbb3dca&". 我有一个字符串“ 4dbb3dca&”。 I am not sure how my string is formmated. 我不确定我的弦是如何固定的。 I want to call rtrim('4dbb3dca&', '&') 我想打电话给rtrim('4dbb3dca&', '&')

--edit: & might be $amp; --edit: &可能是$amp; special character issue 特殊字符问题

But after my string is 4dbb3dc . 但是在我的字符串之后是4dbb3dc

I expect it to be 4dbb3dca . 我希望它是4dbb3dca

Is there any workaround? 有什么解决方法吗? I ried to use & , but then rtrim does nothing and initial string is returned. 我尝试使用& ,但是rtrim不执行任何操作,并且返回了初始字符串。 Is it possible to use rtrim in my situation? 在我的情况下可以使用rtrim吗? Does it depend on php version? 是否取决于php版本?

--- edit --- -编辑-

after some research I realized that I want: I want remove & using & 经过研究后,我意识到我想要:我想要删除&使用& in rtrim() function. rtrim()函数中。 Is this possible? 这可能吗?

Q1 . Q1 Is it possible to use rtrim in my situation? 在我的情况下可以使用rtrim吗? Yes , it is possible to use rtrim() in your situation. 是的 ,您可以根据情况使用rtrim()

Q2 . Q2 Does it depend on php version? 是否取决于php版本? No 没有

You just need to use echo rtrim('4dbb3dca&', '&'); 您只需要使用echo rtrim('4dbb3dca&', '&'); not the & 不是& because 2nd parameter of rtrim works like below- 因为rtrim的第二个参数的工作方式如下-

You can also specify the characters you want to strip, by means of the character_mask parameter. 你也可以指定你想要去除,由character_mask参数的手段的人物 Simply list all characters that you want to be stripped 只需列出您要删除的所有字符

DEMO: https://3v4l.org/anFIR 演示: https : //3v4l.org/anFIR

If you use this below example, then you'll get the same result because it'll try to remove all the given character(s) from the right side of your given string. 如果使用下面的示例,则会得到相同的结果,因为它将尝试从给定字符串的右侧删除所有给定字符。 For Ex- 对于前

echo rtrim('4dbb3dca&;pm', '&');` // will also return 4dbb3dc

The second string you specify in rtim() contains all the extra characters you want to trim. 您在rtim()中指定的第二个字符串包含要修剪的所有其他字符。 So the chars getting trimmed are "&" and "a" (both are indeed included in the second parameter of your rtrim() function call, "&amp" ). 因此,要修剪的字符为"&""a" (这两个确实包含在您的rtrim()函数调用的第二个参数"&amp" )。

To get the result that you want, you should invoke rtrim() this way: 为了获得想要的结果,您应该通过以下方式调用rtrim()

rtrim('4dbb3dca&', '&')

It looks like you're trying to remove an ampersand from the end of the string, potentially in encoded form. 看起来您正在尝试从字符串末尾删除“&”号(可能采用编码形式)。 rtrim doesn't give you enough control to do that, as you've seen, but you can do it with preg_replace . 如您所见, rtrim没有给您足够的控制权来执行此操作,但是您可以使用preg_replace

echo preg_replace('/&(amp;)?$/', '', $string);

The pattern will match an ampersand at the end of the string, optionally followed by amp;. 该模式将在字符串的末尾匹配与号,并可选地在其后加上amp;。

The way the rtrim works remove the last characters at the second argument. rtrim工作方式是删除第二个参数的最后一个字符。

the PHP has split the string to characters and make a check if you have the characters at last of the string. PHP已将字符串拆分为字符,并检查字符串的最后是否包含字符。

if you have two characters from the list of characters the rtrim will remove both 如果您从字符列表中选择了两个字符,则rtrim会删除这两个字符

For example : 例如 :

;&apm it will be the same result like & ;&apm将与&相同的结果

if you want to remove the only ampersand symbol at the end of the string use this code: rtrim('4dbb3dca&','&'); 如果要删除字符串末尾唯一的&符号,请使用以下代码: rtrim('4dbb3dca&','&');

for understand more you can read about it at php.net http://php.net/manual/en/function.rtrim.php 要了解更多信息,可以在php.net http://php.net/manual/en/function.rtrim.php上阅读有关内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM