PHP正则表达式从字符串末尾删除特定部分

时间:2016-10-18 16:10:36

标签: php regex string

我有以下字符串

"DJ Antoine, Conor Maynard - Dancing In The Headlights - teledysk, tekst piosenki"

我想只得到:

"DJ Antoine, Conor Maynard - Dancing In The Headlights"

我使用正则表达式但它不起作用:

^([^\;]+)$ - teledysk, tekst piosenki

2 个答案:

答案 0 :(得分:1)

这是我的两分钱:

php > $s = "DJ Antoine, Conor Maynard - Dancing In The Headlights - teledysk, tekst piosenki";
php > $new_s = explode('-', $s, -1);
php > echo implode('-', $new_s);
DJ Antoine, Conor Maynard - Dancing In The Headlights 

答案 1 :(得分:0)

使用preg_replace()删除字符串的其他部分。正则表达式选择其他部分并将其替换为空。

$str = preg_replace("/-[\w\s,]+$/", "", $str);

请参阅demo

中的结果