开发者

Help with PHP code

This code is giving a syntax error. can anyone tell me where is the problem? thanks in advance.

echo "<div 开发者_JAVA技巧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>";


You have to do escaping. Instead of:

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

write:

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

Rewrite your example to something like this:

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. code is a bit hard to read. Try only using one type of quotes to wrap around string and then another one can be used safely inside of that string.

Also - remember - if you wrap your string in ' or " - you have to escape this character inside of the string by adding backslash in front of it: \

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

There. You had some escaping issues. You were starting some strings with ' and ending them either with " or you were accidentally closing them without escaping


It is a classic case of mixing double quotes and single quotes, and forgetting to escape characters. The string you return also seems to contain one extra </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>";


I'm getting this code:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜