如何在 PHP 中删除字符串末尾的所有特定字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2053830/
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
How do I remove all specific characters at the end of a string in PHP?
提问by ggggggggg
How do I remove the last character only if it's a period?
仅当最后一个字符是句点时,如何删除它?
$string = "something here.";
$output = 'something here';
回答by Alix Axel
回答by ghostdog74
using rtrim replaces all "." at the end, not just the last character
使用 rtrim 替换所有“.” 最后,不仅仅是最后一个字符
$string = "something here..";
echo preg_replace("/\.$/","",$string);
回答by C.O.
To remove the last character only if it's a period and not resorting to preg_replacewe can just treat the string as an array of char and remove the final character if it is a dot.
要仅在最后一个字符是句点而不是诉诸于时才删除最后一个字符,preg_replace我们可以将字符串视为字符数组,如果最后一个字符是点,则将其删除。
if ($str[strlen($str)-1]==='.')
$str=substr($str, 0, -1);
回答by Shahbaz
I know the question is some what old but may be my answer is helpful for someone.
我知道这个问题有些陈旧,但可能我的回答对某人有帮助。
$string = "something here..........";
$string = "something here..........";
ltrimwould remove leading dots. for example:- ltrim($string, ".")
ltrim将删除前导点。例如:-ltrim($string, ".")
rtrimrtrim($string, ".")would remove trailing dots.
rtrimrtrim($string, ".")将删除尾随点。
trimtrim($string, ".")would remove trailing and leading dots.
修剪trim($string, ".")将删除尾随和前导点。
you can also do this by regex
你也可以通过正则表达式来做到这一点
preg_replacewould remove can be used to remove dot/dots at the end
preg_replace willremove 可用于删除末尾的点/点
$regex = "/\.$/"; //to replace single dot at the end
$regex = "/\.+$/"; //to replace multiple dots at the end
preg_replace($regex, "", $string);
I hope it is helpful for you.
我希望它对你有帮助。
回答by Nishad Up
The last character can be removed in different ways, Here is some
可以通过不同的方式删除最后一个字符,这是一些
rtrim():
$output = rtrim($string, '.');
Regular Expression
preg_replace("/.$/", "", $string);
substr() / mb_substr()
echo mb_substr($string, 0, -1);
echo substr(trim($string), 0, -1);
substr() with trim()
echo substr(trim($string), 0, -1);
rtrim():
$output = rtrim($string, '.');
正则表达式
preg_replace("/.$/", "", $string);
substr() / mb_substr()
echo mb_substr($string, 0, -1);
echo substr(trim($string), 0, -1);
substr() 和 trim()
echo substr(trim($string), 0, -1);
回答by Nishal K.R
I know the question is solved. But maybe this answer will be helpful for someone.
我知道问题已经解决了。但也许这个答案会对某人有所帮助。
rtrim()- Strip whitespace (or other characters) from the end of a string
rtrim()- 从字符串的末尾去除空格(或其他字符)
ltrim()- Strip whitespace (or other characters) from the beginning of a string
ltrim()- 从字符串的开头去除空格(或其他字符)
trim()- Strip whitespace (or other characters) from the beginning and end of a string
trim()- 从字符串的开头和结尾去除空格(或其他字符)
For removing special characters from the end of the string or Is the string contains dynamic special characters at the end, we can do by regex.
对于从字符串末尾删除特殊字符或字符串末尾是否包含动态特殊字符,我们可以通过正则表达式来完成。
preg_replace- Perform a regular expression search and replace
preg_replace- 执行正则表达式搜索和替换
$regex = "/\.$/"; //to replace the single dot at the end
$regex = "/\.+$/"; //to replace multiple dots at the end
$regex = "/[.*?!@#$&-_ ]+$/"; //to replace all special characters (.*?!@#$&-_) from the end
$result = preg_replace($regex, "", $string);
Here is some example to understand when $regex = "/[.*?!@#$&-_ ]+$/";is applied to string
这里有一些例子来理解什么时候$regex = "/[.*?!@#$&-_ ]+$/";应用于字符串
$string = "Some text........"; // $resul -> "Some text";
$string = "Some text.????"; // $resul -> "Some text";
$string = "Some text!!!"; // $resul -> "Some text";
$string = "Some text..!???"; // $resul -> "Some text";
I hope it is helpful for you.
我希望它对你有帮助。
Thanks :-)
谢谢 :-)
回答by GreensterRox
Use a combination of strrposand substrto get the position of the last period character and remove it leaving all other characters intact:
使用strrpos和substr的组合来获取最后一个句点字符的位置并将其删除,保留所有其他字符不变:
$string = "something here.";
$pos = strrpos($string,'.');
if($pos !== false){
$output = substr($string,0,$pos);
} else {
$output = $string;
}
var_dump($output);
// $output = 'something here';
回答by Moiz Shafqat Husain
You can use rtrim function of php which allows you to trim the data which exist in last position.
您可以使用 php 的 rtrim 函数,它允许您修剪存在于最后位置的数据。
For example :
例如 :
$trim_variable= rtrim($any_string, '.');
Simplest and fasted way !!
最简单快速的方法!!
回答by Chittaranjan Sethi
Example:
例子:
$columns = array('col1'=> 'value1', 'col2' => '2', 'col3' => '3', 'col4' => 'value4');
echo "Total no of elements: ".count($columns);
echo "<br>";
echo "----------------------------------------------<br />";
$keys = "";
$values = "";
foreach($columns as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
$keys = $keys."'".$x."',";
$values = $values."'".$x_value."',";
echo "<br>";
}
echo "----------------------Before------------------------<br />";
echo $keys;
echo "<br />";
echo $values;
echo "<br />";
$keys = rtrim($keys, ",");
$values = rtrim($values, ",");
echo "<br />";
echo "-----------------------After-----------------------<br />";
echo $keys;
echo "<br />";
echo $values;
?>
Output:
输出:
Total no of elements: 4
----------------------------------------------
Key=col1, Value=value1
Key=col2, Value=2
Key=col3, Value=3
Key=col4, Value=value4
----------------------Before------------------------
'col1','col2','col3','col4',
'value1','2','3','value4',
-----------------------After-----------------------
'col1','col2','col3','col4'
'value1','2','3','value4'

