Reputation: 3
$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
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
Reputation: 15786
$string = "Blue - Art-1011-No.1 [Pinpoint Light Blue]-78";
$needle = "-";
echo substr( $string, 0, strrpos( $string, $needle, 0 ) );
Upvotes: 1