繁体   English   中英

如何删除 php 中字符串末尾附近的分配字符?

[英]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" ; 

我想删除字符串末尾附近显示的字符“ - ”

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

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

怎么了? 虽然我想

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

p/s:我不想使用位置,因为其他字符串可能是 -878、-999、-1234

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

您可以使用 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM