2012-12-30 66 views
0

我正在编写自定义PHP函数,以便更容易地生成使用媒体查询的CSS样式。PHP:将函数的输出插入到字符串中

我的功能越来越重复,所以我创建了第二个函数,我把它放在第一个函数中。第二个函数包含所有重复的代码。

第二个函数产生一个文本字符串,该字符串插入到第一个函数的另一个字符串中。

不过,我觉得最终的输出放置第二个函数文本字符串第一功能串的。我该如何解决这个问题?

这里是我的代码:

<?php 
function css_bgcolor_height_embded($region, $height_value, $mobile_setting, $mobile_custom_height) 
{ 
    function css_height_output($region, $height_value, $class_prefix, $height_division) 
    { 
     echo "$class_prefix" . " ." . $region . "-bgcolor-height-source-element { 
      height: $height_value" . "px; 
      height:" . (($height_value/$height_division)/10) . "rem; 
     }" . " " . " 

     $class_prefix" . " ." . $region . "-bgcolor-height { 
      top:" . "$height_value" . "px; 
      top:" . (($height_value/$height_division)/10) . "rem; 
     } "; 

    } 


    $css_bgcolor_height_embeded .= css_height_output($region, $height_value, '', 1); 


    if ($mobile_setting == '50%') { 
     $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 959px) {" . css_height_output($region, $height_value, '.submenu-devices', 2) . "}"; 

     $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 767px) {" . css_height_output($region, $height_value, '.submenu-portables', 2) . "}"; 

     $css_bgcolor_height_embeded .= css_height_output($region, $height_value, 'submenu-all', 2); 


    } elseif ($mobile_setting == 'custom') { 
     $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 959px) {" . css_height_output($region, $mobile_custom_height, '.submenu-devices', 2) . "}"; 

     $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 767px) {" . css_height_output($region, $mobile_custom_height, '.submenu-portables', 2) . "}"; 

     $css_bgcolor_height_embeded .= css_height_output($region, $mobile_custom_height, '.submenu-all', 2); 

    } 

    echo $css_bgcolor_height_embeded; 
} 

echo css_bgcolor_height_embded('header', 10, 'custom', '100'); 

?> 

这里是我的输出:(对媒体查询的代码错误地放置在它之外[见结束])

.header-bgcolor-height-source-element { 
    height: 10px; 
    height: 1rem; 
} 

.header-bgcolor-height { 
    top: 10px; 
    top: 1rem; 
} 

.submenu-devices .header-bgcolor-height-source-element { 
    height: 100px; 
    height: 5rem; 
} 

.submenu-devices .header-bgcolor-height { 
    top: 100px; 
    top: 5rem; 
} 

.submenu-portables .header-bgcolor-height-source-element { 
    height: 100px; 
    height: 5rem; 
} 

.submenu-portables .header-bgcolor-height { 
    top: 100px; 
    top: 5rem; 
} 

.submenu-all .header-bgcolor-height-source-element { 
    height: 100px; 
    height: 5rem; 
} 

.submenu-all .header-bgcolor-height { 
    top: 100px; 
    top: 5rem; 
} 
@media only screen and (min-width: 0px) and (max-width: 959px) { 
} 
@media only screen and (min-width: 0px) and (max-width: 767px) { 
} 

这里是什么输出应该是:(媒体查询的代码应该放在里面)

.header-bgcolor-height-source-element { 
    height: 10px; 
    height: 1rem; 
} 

.header-bgcolor-height { 
    top: 10px; 
    top: 1rem; 
} 


.submenu-all .header-bgcolor-height-source-element { 
    height: 100px; 
    height: 5rem; 
} 

.submenu-all .header-bgcolor-height { 
    top: 100px; 
    top: 5rem; 
} 
@media only screen and (min-width: 0px) and (max-width: 959px) { 

.submenu-devices .header-bgcolor-height-source-element { 
    height: 100px; 
    height: 5rem; 
} 

.submenu-devices .header-bgcolor-height { 
    top: 100px; 
    top: 5rem; 
} 

} 
@media only screen and (min-width: 0px) and (max-width: 767px) { 
    .submenu-portables .header-bgcolor-height-source-element { 
    height: 100px; 
    height: 5rem; 
} 

.submenu-portables .header-bgcolor-height { 
    top: 100px; 
    top: 5rem; 
} 
} 

(最终代码会然后插入到Drupal函数中。但是,我忽略了Drupal元素,因为我认为这更像是一个普通的PHP问题)。

+1

而不是在函数中回显数据,将其返回给变量,然后在适当的位置回显变量。除非我误解你的问题^。^ –

+0

@MattClark作为回答发布。 – Barmar

+0

'$ var。= my_function(..);'其中'my_function(..)'返回一个字符串 – twodayslate

回答

3

在你的css_height_output函数中使用echo的Insead,你应该使用return来代替它。问题是echo会立即输出该字符串,而不是将其插入到您要查找的字符串中。

ECHO功能将在那一刻取出该文本并将其输出到文本正文中,然后返回将该数据发回给它的内容,在您的情况下,将其添加到您的文本末尾$css_bgcolor_height_embeded变量。

function css_height_output($region, $height_value, $class_prefix, $height_division) 
{ 
    return "$class_prefix" . " ." . $region . "-bgcolor-height-source-element { 
     height: $height_value" . "px; 
     height:" . (($height_value/$height_division)/10) . "rem; 
    }" . " " . " 

    $class_prefix" . " ." . $region . "-bgcolor-height { 
     top:" . "$height_value" . "px; 
     top:" . (($height_value/$height_division)/10) . "rem; 
    } "; 

} 
+0

谢谢!我无法相信,一个小小的字让我如此悲痛! –

+0

你会惊讶我有多少个字母案例proBlems ... xD快乐编码! –

2

试试这个...

<?php 
function css_bgcolor_height_embded($region, $height_value, $mobile_setting, $mobile_custom_height) 
{ 
    function css_height_output($region, $height_value, $class_prefix, $height_division) 
    { 
    return "$class_prefix" . " ." . $region . "-bgcolor-height-source-element { 
     height: $height_value" . "px; 
     height:" . (($height_value/$height_division)/10) . "rem; 
    }" . " " . " 

    $class_prefix" . " ." . $region . "-bgcolor-height { 
     top:" . "$height_value" . "px; 
     top:" . (($height_value/$height_division)/10) . "rem; 
    }\n\n"; 

    } 


    $css_bgcolor_height_embeded = css_height_output($region, $height_value, '', 1); 


    if ($mobile_setting == '50%') { 
    $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 959px) {" . css_height_output($region, $height_value, '.submenu-devices', 2) . "}"; 

    $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 767px) {" . css_height_output($region, $height_value, '.submenu-portables', 2) . "}"; 

    $css_bgcolor_height_embeded .= css_height_output($region, $height_value, 'submenu-all', 2); 


    } elseif ($mobile_setting == 'custom') { 
    $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 959px) {" . css_height_output($region, $mobile_custom_height, '.submenu-devices', 2) . "}"; 

    $css_bgcolor_height_embeded .= "@media only screen and (min-width: 0px) and (max-width: 767px) {" . css_height_output($region, $mobile_custom_height, '.submenu-portables', 2) . "}"; 

    $css_bgcolor_height_embeded .= css_height_output($region, $mobile_custom_height, '.submenu-all', 2); 

    } 

    return $css_bgcolor_height_embeded; 
} 

echo css_bgcolor_height_embded('header', 10, 'custom', '100'); 

?> 

注意用回,而不是回声。 Echo在评估时将其打印出来,

+0

谢谢!也感谢\ n建议使输出更具可读性。 –

相关问题