J. Doe
J. Doe

Reputation: 9

PHP regex remove specific part from end of string

I have the following string

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

and I would like to get only:

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

I use regex but it not working:

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

Upvotes: 0

Views: 66

Answers (2)

Mohammad
Mohammad

Reputation: 21489

Use preg_replace() to remove additional part of string. The regex select additional part and replace it with empty.

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

See result in demo

Upvotes: 0

JazZ
JazZ

Reputation: 4579

Here is my two cents :

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 

Upvotes: 1

Related Questions