WorldDrknss Posted March 27, 2009 Share Posted March 27, 2009 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 Quote Link to comment Share on other sites More sharing options...
ober Posted March 27, 2009 Share Posted March 27, 2009 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. Quote Link to comment Share on other sites More sharing options...
Maq Posted March 27, 2009 Share Posted March 27, 2009 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? Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted March 27, 2009 Share Posted March 27, 2009 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; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 27, 2009 Share Posted March 27, 2009 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. Quote Link to comment Share on other sites More sharing options...
WorldDrknss Posted March 27, 2009 Author Share Posted March 27, 2009 Its being generated by icecast streaming server. Quote Link to comment Share on other sites More sharing options...
Maq Posted March 27, 2009 Share Posted March 27, 2009 @taquitosensei, If he had the artist and song title in separate string variables, then he wouldn't have even asked this question. Quote Link to comment Share on other sites More sharing options...
WorldDrknss Posted March 27, 2009 Author Share Posted March 27, 2009 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]; Quote Link to comment Share on other sites More sharing options...
Salkcin Posted March 28, 2009 Share Posted March 28, 2009 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"; ?> Quote Link to comment Share on other sites More sharing options...
WorldDrknss Posted March 28, 2009 Author Share Posted March 28, 2009 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. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 29, 2009 Share Posted March 29, 2009 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"; ?> Quote Link to comment Share on other sites More sharing options...
WorldDrknss Posted March 29, 2009 Author Share Posted March 29, 2009 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 Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 29, 2009 Share Posted March 29, 2009 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"; ?> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 29, 2009 Share Posted March 29, 2009 ?= is a positive lookahead ?: is a non-capture, (.*?) is the same as (?:.*?) except you can referback to the one with ?: Quote Link to comment Share on other sites More sharing options...
WorldDrknss Posted March 29, 2009 Author Share Posted March 29, 2009 ?= is a positive lookahead ?: is a non-capture, (.*?) is the same as (?:.*?) except you can referback to the one with ?: Thanks MadTechie, that helped me understand the regular expression a little bit more. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 29, 2009 Share Posted March 29, 2009 ?: is a non-capture, (.*?) is the same as (?:.*?) except you can referback to the one with ?: lol i messed that up you can NOT referback to the one with ?: Quote Link to comment Share on other sites More sharing options...
WorldDrknss Posted March 29, 2009 Author Share Posted March 29, 2009 ?: is a non-capture, (.*?) is the same as (?:.*?) except you can referback to the one with ?: lol i messed that up you can NOT referback to the one with ?: Ok, I get it now. Since I know what they mean now I can start using them. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.