sd1sd1
sd1sd1

Reputation: 1048

php preg_replace trailing minus sign

I try to remove a trialing minus sign of a string.

I've got the following code:

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

I also tried:

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

and:

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

But that does not seem to work, any ideas what I do wrong? That's should be simple issue but somehow I can't get it to work.

Upvotes: 0

Views: 1117

Answers (2)

Orangepill
Orangepill

Reputation: 24645

just use rtrim to get any trailing minus signs

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

for multiline you can do preg_replace but make sure to account for the trailing spaces

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

Upvotes: 1

CodeAngry
CodeAngry

Reputation: 12985

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

or

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

Upvotes: 0

Related Questions