0

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
4
  • split the string between "-" and keep only the first two elements. Commented Oct 18, 2016 at 16:11
  • And using regexp? For future Commented Oct 18, 2016 at 16:12
  • How are you using ^([^\;]+)$? ^ and $ are the start and end of the string, you can't have them in the middle. regex101.com/r/nRhNYv/1 (if that string is your regex, your PHP usage is missing from question). Commented Oct 18, 2016 at 16:14
  • The function for matching regex in php is preg_match() Commented Oct 18, 2016 at 16:15

2 Answers 2

1

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 
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.