2013-06-12 35 views
0

對於較差的標題,我表示歉意。考慮下面的代碼:將PHP + CSS標準化爲不使用字符串解析

<?php 

    $css = 'foo-'.$data['style']; 
    $html= "<table><tr><td class='{$css}>styled! /> </tr> </table>; 
?> 

其中[style]返回值:'bar','baz','apple'

我怎麼能正確刪除了這一點,並使其HTML + CSS標準,而不是這個奇怪的PHP解析的?

澄清'奇怪':我無法在瀏覽器中將其作爲標準HTML頁面打開,這正是我想要做的。

+5

你是不是在這裏和那裏缺乏一些引號? –

+0

'$ data ['style']'是一個數組嗎? – landons

+1

我不明白這個問題。這個奇怪的PHP解析是怎麼回事?你只是使用一個變量作爲填充。 –

回答

0
<table><tr><td class='foo-<?= $data["style"] ?>' /> </tr> </table> 

<table><tr><td class='<?= "foo-".$data["style"] ?>' /> </tr> </table> 

可以做

$style = "foo-".$data["style"]; 
?> 
<table><tr><td class='<?= $style ?>' /> </tr> </table> 
0

代碼:

<?php 
$css = 'foo-'.$data['style']; 
$html= "<table><tr><td class='{$css}>styled! /> </tr> </table>; 
?> 

似乎有一些錯誤,請更正此:

<?php 
$css = 'foo-'.$data['style']; 
$html= "<table><tr><td class=".$css.">styled! </td></tr></table>"; 
echo $html; 
?> 

否則你可以在HTML + CSS標準寫:

<?php 
$css = 'foo-'.$data['style']; 
?> 

<table> 
    <tr> 
    <td class="<?php echo $css; ?>">styled!</td> 
    </tr> 
</table> 
+0

我不確定請在您的本地機器上檢查。 – Chinmay235

0

我覺得 '酒吧', '巴茲', '蘋果' 是分開的CSS類。這將與foo-連接。我的意思是foo-bar,foo-baz等。如果是這樣,那麼你必須像這樣爆炸每個班級。

<?php 
$css_arr = explode(',',$data['style']); 
$css_all_class = implode(' foo-',$css_arr); 
$css_all_class = "foo-".$css_all_class ; 
//$css_all_class = " foo-bar foo-baz foo-apple" 
?> 

<table> 
    <tr> 
     <td class="<?php echo $css_all_class; ?>">styled!</td> 
    </tr> 
</table> 
+0

你想要這樣的事情嗎? 「

風格!
」。如果是,它應該工作。 –