PHP:用“Space”替换“+”符号

时间:2012-12-18 08:08:26

标签: php wordpress

我正在使用echo标签输出用户在我的网站上选择的一些字体属性,通过 Wordpress > Appreances > 主题选项

一旦他们从这个页面上的某个选择菜单中选择了他们喜欢的字体,它就会调到前端的源代码中,如下所示:

的header.php

<style>
<?php $typography = of_get_option('main-text');
if ($typography) {          
echo 'p {
    font: ' . $typography['size']. ' '.$typography['face'] . '; 
    font-style: ' . $typography['style'] . '; 
    color: '.$typography['color'].';    
}';

$typography = str_replace(' ','+',$typography); 
} 
?>
</style>

由于选择菜单中的选项包括 Google网络字体,因此某些字体包含+个符号,我想用简单的space替换它们代替。

由于我对PHP有点不熟悉,我想知道如何正确地写出类似

的内容
$typography = str_replace(' ','+',$typography); 

对于上面的脚本,正如我所尝试的那样/我放置它的地方,不起作用。

谢谢。

2 个答案:

答案 0 :(得分:3)

  $typography = str_replace(' ','+',$typography); 

应该是

 $typography = str_replace('+',' ',$typography); 

为了用+

替换space

http://php.net/manual/en/function.str-replace.php

答案 1 :(得分:1)

str_replace函数的前2个参数是向后的。此外,如果您希望它在回显之前替换,那么您必须在回显之前进行替换。

如果你只需要对'face'键进行替换,那么你可以这样做:

<style>
<?php $typography = of_get_option('main-text');
if ($typography)
{    
    $typography['face'] = str_replace('+', ' ', $typography['face']); 

    echo 'p {
        font: ' . $typography['size']. ' '.$typography['face'] . '; 
        font-style: ' . $typography['style'] . '; 
        color: '.$typography['color'].';    
    }';
} 
?>
</style>