【问题标题】:Exploding Song title爆炸的歌名
【发布时间】:2016-01-20 09:38:52
【问题描述】:

我在爆炸这个标题时遇到了一些问题歌曲艺术家 - 歌曲名称我正在使用以下代码并且运气不太好。

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

当我显示时它应该是这样的。

但相反,它返回的东西看起来像这样。

【问题讨论】:

  • 你的错误或问题是什么?明确具体
  • 我担心这是一个糟糕的方法:如果艺术家或歌曲标题都包含 - 你的方法会失败。如果它们不包含 -,请描述您的期望以及您得到的。我不明白你在用正则表达式做什么或你试图实现什么。
  • @ErwinMoller 再次查看更新..
  • 只需输出你填写的变量:$artist 和 $song。它们是否包含您的期望?也许你只填充艺术家?
  • 只是调试。 echo $links、echo $str[0]、echo $str[1] 等等。也许 - 不是 - 你使用的? (unicode?)

标签: php


【解决方案1】:

我发现了一些你可能感兴趣的东西:

在您的原始代码中,在$title2=$title-&gt;plaintext; 下添加echo htmlentities($title)."&lt;br&gt;";。像这样

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

这就是爆炸不起作用的原因。你可能会在&amp;#8211;上爆炸

在这里检查过,它似乎工作。

抱歉所有的编辑,我很难显示&amp;#8211; :-)

【讨论】:

  • 这已解决,非常感谢 Erwin,非常幸运您决定抽出时间来帮助我!
【解决方案2】:

您可以使用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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2022-01-18
    相关资源
    最近更新 更多