2016-01-20 41 views
0

我有一些問題爆炸此標題歌手藝術家 - 歌名我使用下面的代碼,並沒有太多運氣。爆炸歌曲標題

$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>'; 

當我顯示時它應該看起來像這樣。 enter image description here

但相反,它返回的東西看起來像這樣。 enter image description here

+0

請告訴我UR錯誤或問題?清楚並具體說明 – Trix

+0

@Trix view update .. – Ritzy

+0

這是一個我很害怕的不好的方法:如果藝術家或歌詞都包含 - 你的方法失敗了。如果他們不包含 - ,請描述你期望的和你得到的。我不明白你在用正則表達式做什麼,或者你試圖達到什麼目的。 –

回答

1

我發現一些您可能感興趣的:

添加echo htmlentities($title)."<br>";$title2=$title->plaintext;下,在原始代碼。像這樣

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

給我:(例如:)

<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; 

這就是爲什麼爆炸沒有工作。你可能會因爲爆炸而離開&#8211;

檢查過它,它似乎工作。

對不起,所有的編輯,我有一個很難顯示&#8211; :-)

+0

這固定它非常感謝你Erwin,非常幸運,你決定把你的時間,以幫助我出去! – Ritzy

0

您可以使用trim,以消除任何不必要的空格:

<?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>