php preg_replace尾随减号

时间:2013-07-09 14:47:11

标签: php preg-replace

我尝试删除字符串的试验减号。

我已获得以下代码:

$name = preg_replace("/\-$/ismU", "", trim($name));

我也尝试过:

$name = preg_replace("/\\\-$/ismU", "", trim($name));

$name = preg_replace("/-$/ismU", "", trim($name));

但这似乎不起作用,任何想法我做错了什么?这应该是一个简单的问题,但不知怎的,我无法让它发挥作用。

2 个答案:

答案 0 :(得分:1)

只需使用rtrim获取任何尾随减号

 $name = rtrim(trim($name), "-");

对于多行,您可以执行preg_replace但请确保考虑尾随空格

 $name = preg_replace('/- *$/ismU', "", trim($name));

答案 1 :(得分:0)

$name = preg_replace("/\\-$/ismU", "", trim($name)); // double quotes, escape \

$name = preg_replace('/\-$/ismU', "", trim($name)); // double quotes, escape \ (works)
$name = preg_replace('/\\-$/ismU', "", trim($name)); // double quotes, escape \ (proper)