开发者

How do I add single quote or double quote if content have a white space

I try to make dynamic CSS using PHP.

Example on font-family

$one = 'Times New Roman, Times, se开发者_Go百科rif';
$two = 'Lucida Sans Unicode, Lucida Grande, sans-serif';

On style.php

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

Here I want to add a single quote or double quote to the Lucida Sans Unicode, Lucida Grande

So the ouput should be

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

Let me know how to replace the font with a quote


A bit more functional :-)

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

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


$two = '"Lucida Sans Unicode", "Lucida Grande", sans-serif';

then

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


$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

Edit:
I didn't think of it, but indeed, adding the quotes to the variable $two (where needed) might do the trick... What happened to my KISS?...


Could you not just do:

body { font-family:"<?php echo strpos(' ', $two) !== false ? '"'.$two.'"' : $two; ?>"; }


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜