0

I am having some issues exploding this title Song Artist – Song Name I am using the following code and having not very much luck.

$title2 = $html2->find('header.section-header h2',0);
$links = $title2->plaintext;
$str = explode ("–", $links);
$artist = preg_replace('#\[[a-zA-Z].*\]#','',$str[0]);
$song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);
print '<div class="song"> <div class="options"> <a class="play" href="'.$url.'"  data-url="'.$url.'" data-title="'.$artist.'"> </a> <a class="download" href="'.$url.'"> </a> </div> <div class="info"> <a class="direct" href="'.$url.'"> <div class="artist">'.$artist.'</div> <div class="title">A Rainy Night In Harlem (Freestyle)</div> </a> </div> </div>';

It should look like this when I display. enter image description here

But instead it returns something that looks like this. enter image description here

Ritzy
  • 379
  • 1
  • 10
  • Whats ur error or problem? be clear and specifc – Peyman Mohamadpour Jan 20 '16 at 09:41
  • This is a poor approach I fear: If both the artist or the songtitle contains a - your approach fails. In case they don't contain a -, describe please what you expect, and what you get. I don't understand what you are doing with the regex or what it is you try to achieve. – Erwin Moller Jan 20 '16 at 09:42
  • @ErwinMoller once again, view update.. – Ritzy Jan 20 '16 at 09:43
  • 1
    simply output the variables you filled: $artist and $song. DO they contain what you expect? Maybe you only fill artist? – Erwin Moller Jan 20 '16 at 09:50
  • @ErwinMoller it does not explode the song name at all.. – Ritzy Jan 20 '16 at 09:53
  • @ErwinMoller view here http://dl.wubstub.com/stream/index.php?a=Angel+Gold+Ft.+Ty+Dolla+Sign%2C+BigTC+%26amp%3Bamp%3B+TeeCee4800+&s=+No+No+No&mp3=http://www.dopefile.me/mp3embed-xni34a6ptemt.mp3 – Ritzy Jan 20 '16 at 09:54
  • 1
    Just debug. echo $links, echo $str[0], echo $str[1], etc. Maybe the - isn't the - you use? (unicode?) – Erwin Moller Jan 20 '16 at 09:54
  • @ErwinMoller I've been messing with all of those things, but no luck.. I don't know why it's not exploding it.. makes no sense to me. – Ritzy Jan 20 '16 at 09:58
  • If you KNOW for sure $links contain a - it should explode. I bet the "-" you use in your explode doesn't match the one in $links. Have look here: http://stackoverflow.com/questions/9361303/can-i-get-the-unicode-value-of-a-character-or-vise-versa-with-php try the accepted anser, function _uniord($c) to see if the characters have the same code. – Erwin Moller Jan 20 '16 at 10:02
  • @ErwinMoller I created a fiddle with all of my code please check it.. http://viper-7.com/hBZfzX I am getting so confused :| if it's not in the foreach you can explode it just fine.. so I am SURE it's using that - bracket – Ritzy Jan 20 '16 at 10:10
  • I'll check the code locally. fiddle gives me errors: Fatal error: Uncaught exception 'GearmanException' with message 'Failed to set exception opti.... etc. I'll be back soon – Erwin Moller Jan 20 '16 at 10:19
  • @ErwinMoller thanks! – Ritzy Jan 20 '16 at 10:19
  • I'll post an "answer" because it doesn't fit here. – Erwin Moller Jan 20 '16 at 10:41
  • Have you confirmed that the dash character is actually a `-`? There are a number of dashes and hyphens in the UTF-8 character set; if it's one of those, then your explode won't see them. You'll need to either ensure that your input text contains only one specific kind of dash, or else use a `preg_split()` to split on any of them. Also be aware that some song titles and artist names may also contain dashes, so be careful about using it as an arbitrary split character. – Simba Jan 20 '16 at 10:53
  • Please consider upvoting helpful answers to be thankful of time and effort people invest on your problems – Peyman Mohamadpour Feb 06 '16 at 14:33

2 Answers2

1

I found something that might interest you:

Add echo htmlentities($title)."<br>"; under $title2=$title->plaintext;, IN YOUR ORIGINAL code. Like this

$title2 = $title->plaintext;
echo htmlentities($title)."<br>";

Gives me: (for example:)

<h2 itemprop="name"><a href="http://www.DailyNewSounds.com/singles">Chris Brown &#8211; You Make Me This Way (I Got You) (LQ)</a></h2>

No - but a &#8211;

That is why the explode didn't work. You might get away with exploding on &#8211;

Checked it over here, and it seems to work.

Sorry for all the edits, I had a hard time displaying &#8211; :-)

Erwin Moller
  • 2,375
  • 14
  • 22
  • This fixed it thank you so much Erwin, extremely blessed that you decided to take the time out of your day to help me out! – Ritzy Jan 20 '16 at 23:36
0

You may use trim to remove any unwanted whitespaces:

<?php
$title2 = $html2->find('header.section-header h2',0);
$links = $title2->plaintext;
$str = explode ("–", $links);
$artist = trim($str[0]);
$song = trim($str[1]);
?>
<div class="song">
    <div class="options">
        <a class="play" href=""  data-url="" data-title=""></a>
        <a class="download" href=""></a>
    </div>
    <div class="info">
        <a class="direct" href="">
            <div class="artist"><?php echo $artist;?></div>
            <div class="title"><?php echo $song;?></div>
        </a>
    </div>
</div>
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100