Tran Van Tin
Tran Van Tin

Reputation: 3

How can I remove assign characters at near the end of a string in php?

$text_one = "Blue - Art-1011-No.1 [Pinpoint Light Blue]-78" ; 

I want to remove character " - " show in the near the end of string

$regex = "/[.*?!@#$&-_ ]+$/";
$result = preg_replace($regex, "", $text_one);

$result =  Blue - Art-1011-No.1 [Pinpoint Light Blue

what is the wrong? while i want to

$result = Blue - Art-1011-No.1 [Pinpoint Light Blue]

p/s: i dont want using possition because other string maybe -878, -999, -1234

Upvotes: 0

Views: 48

Answers (2)

Rahul Gupta
Rahul Gupta

Reputation: 1041

You can use PHP strrpos() function :

$text_one = "Blue - Art-1011-No.1 [Pinpoint Light Blue]-78" ; 
$needle = "-";
$result =  substr( $text_one, 0, strrpos( $text_one, $needle, 0 ) );
echo $result;

Upvotes: 0

Gerard
Gerard

Reputation: 15786

$string = "Blue - Art-1011-No.1 [Pinpoint Light Blue]-78";
$needle = "-";
echo substr( $string, 0, strrpos( $string, $needle, 0 ) );

Upvotes: 1

Related Questions