2011-02-02 89 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);