2011-07-27 180 views
3

此代碼給出語法錯誤。誰能告訴我問題在哪裏?提前致謝。幫助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>"; 
+0

,你的問題是......? –

+0

對不起,想知道這段代碼中的問題在哪裏。謝謝 – maxlk

+0

它是一個逃避問題。閱讀我的答案,我爲你解決並告訴你什麼是錯的。 – Chris

回答

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

附:代碼有點難以閱讀。只嘗試使用一種類型的引號來環繞字符串,然後可以在該字符串內安全地使用另一種引號。

此外 - 記住 - 如果你包裝你的字符串中「或」 - 你必須轉義字符串內這個角色在它前面加上反斜線:\

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

+1

非常感謝。我現在明白了。 – maxlk

0

我得到這個代碼:

echo " 
".html_entity_decode(substr($video['video_title'],0,100)).".. 
".html_entity_decode(substr($video['video_desc'],0,100))." 
View Count: ".$video['views']." 
"; 
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>"; 

有。你有一些逃避的問題,你開始一些字符串'結束它們或',或者你不小心關閉它們而沒有逃脫

+0

感謝這個作品 – maxlk

1

這是一個經典的混合雙引號和單引號,忘記escap e字符。 返回的字符串也似乎包含一個額外的</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>";