Jump to content

[SOLVED] Split


WorldDrknss

Recommended Posts

I need help splitting a string, so that it will split correctly depending on the string it receives.

 

Example

$song = "Blink - 182 - Whats my age"
$song = "Jon Lojoie - Sunday"

I am trying to explode using - but it will return:

182

Blink

 

and

 

Sunday

Jon Lojoie

 

instead of

 

Whats my age

Blink - 182

 

and

 

Sunday

Jon Lojoie

Link to comment
Share on other sites

Explode the string, count the number of elements in the array, and then put the first 2 together if there are more than 2 elements.  If there will be instances where there will be more hyphens in the string, it will get more tricky, but that's the basic way to handle this.

Link to comment
Share on other sites

Explode the string, count the number of elements in the array, and then put the first 2 together if there are more than 2 elements.  If there will be instances where there will be more hyphens in the string, it will get more tricky, but that's the basic way to handle this.

 

What if there is a hyphen in the song title?

 

WorldDrknss, how are these song titles being generated?

Link to comment
Share on other sites

How are you assembling the string?  You could replace hyphens in the individual parts with another character pipe |? Then assemble your string.  Then split it and replace the | with -.

$string1="Blink - 182"; 
$string2="What\'s my age"; 

$string1=str_replace("-", "|", $string1); 
etc.. 
etc..
$string=$string1." - ".$string2; 
$string_split=split(" - ",$string); 
foreach($string_split as $string) 
{ 
                 $string_with_hyphen=str_replace("|", " - ", $string); 
                 echo $string_with_hyphen; 
}

Link to comment
Share on other sites

Unfortunately there is not going to be a 100% effective, programatical solution to your problem. You could just as easily have a track with a hyphen in it. I have run into this problem as well with regard to parsing artist/track names. My suggestion is to create a "white" list of artist exceptions which include the hyphen.

 

When processing the data see if the artist matches anything in the white list, if so, parse using the first hyphen after the name in the white list. If not, then split using the hyphen. If there are only two pieces of data after the split, you're good to go. If there are more than two elements after the split than flag that record as an error (or just assume everything after the first hyphen is the title). Really just depends on how accurate the data needs to be and the time you can invest to correct it.

Link to comment
Share on other sites

Unfortunately there is not going to be a 100% effective, programatical solution to your problem. You could just as easily have a track with a hyphen in it. I have run into this problem as well with regard to parsing artist/track names. My suggestion is to create a "white" list of artist exceptions which include the hyphen.

 

When processing the data see if the artist matches anything in the white list, if so, parse using the first hyphen after the name in the white list. If not, then split using the hyphen. If there are only two pieces of data after the split, you're good to go. If there are more than two elements after the split than flag that record as an error (or just assume everything after the first hyphen is the title). Really just depends on how accurate the data needs to be and the time you can invest to correct it.

 

mjdamato, could you write me a quick example on how to do this.

$str = "Blink - 182 - Song Title";
$match = "Blink - 182"; //This will be pulled from a whitelist
$format = ereg("($match) - (.*)", $str, $regs);
$artist = $regs[1];
$song = $regs[2];

Link to comment
Share on other sites

about using regular expressions, you could try something like this:

<?php

$song = "Blink - 182 - Whats my age";
//$song = "Some - Artist - Some - Tune";
//$song = "Some Artist - Some - Tune";

list($artist, $song) = explode('|', preg_replace('/\s*-?\s*([^-\s]+(?:\s*-\s*)?(?:[^-]+)?)(\s+-\s+)+(.+)\s*-?\s*/', '\\1|\\3', $song));
echo "Band: $artist<br />Song: $song";

?>

Link to comment
Share on other sites

about using regular expressions, you could try something like this:

<?php

$song = "Blink - 182 - Whats my age";
//$song = "Some - Artist - Some - Tune";
//$song = "Some Artist - Some - Tune";

list($artist, $song) = explode('|', preg_replace('/\s*-?\s*([^-\s]+(?:\s*-\s*)?(?:[^-]+)?)(\s+-\s+)+(.+)\s*-?\s*/', '\\1|\\3', $song));
echo "Band: $artist<br />Song: $song";

?>

 

Thanks, I am going to test this right now, but leads to me to another question. Could you tell me what the colon does in a regular expression. I can't find any information relating to what it does.

 

Edit: It works, thanks, I appreciate that work that you did.

Link to comment
Share on other sites

Personally i would do something like this

 

<?php
$song = "Blink - 182 - Whats my age";
preg_match('/(^.*)(?= - ) - (.*)$/im', $song, $regs))
$artist = $regs[1];
$song= $regs[2];
echo "Band: $artist<br />Song: $song";
?>

 

could you please explain the difference.

 

I don't understand the ?: and ?= in regex expressions

Link to comment
Share on other sites

i got it going this way , why do i have to set the array different to see the result?

 

and i can not use '' single quotes.

<?php
$song = "Blink - 182 - Whats my age";
preg_match_all("/(^.*)(?= - ) - (.*)$/im", $song, $regs);

$artist=$regs[1][0];
$song=$regs[2][0];

echo "Band: $artist<br />Song: $song";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.