2011-02-02 30 views
1

我嘗試使用PHP進行動態CSS如果內容有空白,如何添加單引號或雙引號

例如在font-family

$one = 'Times New Roman, Times, serif'; 
$two = 'Lucida Sans Unicode, Lucida Grande, sans-serif'; 

在style.php

body { font-family:<?php echo $two; ?>; } 

在這裏,我想添加一個單引號或雙引號到Lucida Sans UnicodeLucida Grande

所以輸出中應

body { font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif; } 

讓我知道如何與報價替換的字體

+0

你可以引用字符串的每個元素,你知道嗎? – SilentGhost 2011-02-02 15:09:48

回答

2

更多的功能有點:-)

function put_quotes ($name) { 
    $name = trim($name); 
    return strpos($name, ' ') ? '"' . $name . '"' : $name; 
} 

$css = implode(', ', array_map('put_quotes', explode(',', $one))); 
0

難道你不只是做:

body { font-family:"<?php echo strpos(' ', $two) !== false ? '"'.$two.'"' : $two; ?>"; } 
1
$two = '"Lucida Sans Unicode", "Lucida Grande", sans-serif'; 

然後

body { font-family:<?php echo $two; ?>; } 
+0

在db上我沒有報價存儲字體列表 – wow 2011-02-02 14:57:16

+1

他們改變了嗎?看起來像不必要的處理。存儲他們的報價... – labue 2011-02-02 14:58:44

1
$two = 'Lucida Sans Unicode, Lucida Grande, sans-serif'; 
$aux = explode(',',$two); 
foreach($aux as &$f){ 
    $f = trim($f); 
    if(strpos($f,' ') !== FALSE) 
     $f = '"' . $f . '"'; 
} 

echo implode(', ',$aux); // "Lucida Sans Unicode", "Lucida Grande", sans-serif 

編輯:
我沒想到的,但事實上,添加引號變量$two(如有必要)可能做的伎倆......發生了什麼事我KISS?...

0
$two_a = explode(",", $two); 
foreach($two_a as $str) { 
    $str = "'".$str."'"; 
} 
$two = implode(",", $two_a);