PHP代码帮助


Help with PHP code

此代码出现语法错误。有人能告诉我问题出在哪里吗?提前谢谢。

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

您必须执行转义。代替:

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

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

那里。你有一些逃避问题。您将一些字符串以'开头,然后以"结尾,或者您意外地在未转义的情况下关闭了它们

这是一个混合双引号和单引号,忘记转义字符的经典案例。您返回的字符串似乎还包含一个额外的</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>";

我得到的代码是:

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