帮助PHP代码

时间:2011-07-27 03:20:02

标签: php

此代码提供语法错误。谁能告诉我问题在哪里?提前致谢。

echo "<div class='cvtitle'><div><a class="bloc_ca" href="'.$video['video_id'].'_'.str_replace(" ","-",substr(html_entity_decode($video['video_title']),0,20)).'.html"><b>".html_entity_decode(substr($video['video_title'],0,100))."..</b></a></div><div class='cvdisc'><span style='word-break:wrap'>".html_entity_decode(substr($video['video_desc'],0,100))."</span></div><div class='cvviews'> View Count: <b>".$video['views']."</b></div></div></div>";

4 个答案:

答案 0 :(得分:3)

你必须逃避。而不是:

echo 'some text' . "aaaa"aaaa";

写:

echo 'some text' . "aaaa\"aaaa";

将您的示例重写为以下内容:

echo "<div class='cvtitle'><div><a class=\"bloc_ca\" href=\"" . $video['video_id'] 
. '_' . str_replace(" ","-",substr(html_entity_decode($video['video_title']),0,20))
. '.html"><b>'
. html_entity_decode(substr($video['video_title'],0,100))
. "..</b></a></div><div class='cvdisc'><span style='word-break:wrap'>" 
. html_entity_decode(substr($video['video_desc'], 0, 100))
. '</span></div><div class="cvviews"> View Count: <b>'
. $video['views']
. '</b></div></div></div>';

P.S。代码有点难以阅读。尝试仅使用一种类型的引号来包裹字符串,然后可以在该字符串内安全地使用另一种引号。

另外 - 记住 - 如果你把你的字符串换成'或' - 你必须通过在它前面添加反斜杠来转义字符串中的这个字符:\

http://php.net/manual/en/language.types.string.php

答案 1 :(得分:2)

echo '<div class=\'cvtitle\'><div><a class="bloc_ca" href="'.$video['video_id'].'_'.str_replace(" ","-",substr(html_entity_decode($video['video_title']),0,20)).'.html"><b>"'.html_entity_decode(substr($video['video_title'],0,100))."..</b></a></div><div class='cvdisc'><span style='word-break:wrap'>".html_entity_decode(substr($video['video_desc'],0,100))."</span></div><div class='cvviews'> View Count: <b>".$video['views']."</b></div></div></div>";

有。你有一些逃避问题。你正在用'并以“结束它们”开始一些字符串,或者你不小心关闭它们而没有逃避

答案 2 :(得分:1)

这是混合双引号和单引号并忘记转义字符的经典案例。 您返回的字符串似乎还包含一个额外的</div>

echo '<div class="cvtitle">
    <div>
        <a class="bloc_ca" href="'. $video['video_id'] . '_' . str_replace(' ','-',substr(html_entity_decode($video['video_title']),0,20)) . '.html">
            <b>' . html_entity_decode(substr($video['video_title'],0,100)) . "..</b></a>
    </div>
    <div class='cvdisc'>
        <span style='word-break:wrap'>" . html_entity_decode(substr($video['video_desc'],0,100))."</span>
    </div>
    <div class='cvviews'> 
        View Count: <b>".$video['views']."</b>
    </div>
  </div>";

答案 3 :(得分:0)

我收到了这段代码:

  echo "
".html_entity_decode(substr($video['video_title'],0,100))."..
".html_entity_decode(substr($video['video_desc'],0,100))."
View Count: ".$video['views']."
";