将PHP函数参数组合成字符串

将PHP函数参数组合成字符串,php,function,arguments,Php,Function,Arguments,我有几个对getContent()函数的调用,其中的参数是使用func\u get\u args()拉入函数的。有几个参数保持不变,我想将它们设置为变量,以防止在所有这些函数调用中重复参数 以下是具有完整参数的函数调用示例: <? getContent( "article", "display:list", "find_series:team", "find_category:pastors-team", "order:series", "f

我有几个对
getContent()
函数的调用,其中的参数是使用
func\u get\u args()
拉入函数的。有几个参数保持不变,我想将它们设置为变量,以防止在所有这些函数调用中重复参数

以下是具有完整参数的函数调用示例:

<? getContent(
    "article",
    "display:list",
    "find_series:team",
    "find_category:pastors-team",
    "order:series",
    "find_parent_category:team",
    "show:<div class='box box_stories'>",
    "show:<div class='box_pad box_img bwWrapper'>",
    "show:<div class='box_inner_pad'>",
    "show:<h3><a href='/staff-detail/__slug__'>__authorX__</a></h3>",
    "show:<p class='teamsummary'>__summary__</p>",
    "show:<p class='phone_icon'>__authorXworkphone__</p>",
    "show:</div>",
    "show:</div>",
    "show:</div>"
); 
?>

不幸的是,这是不可能的。您现在要传递的是
文章
显示
,等等和
$myStyle
(应该是一个数组)。您将得到错误,因为您没有传递足够的参数


如果可以,请检查并查看
getContent()
是否有默认参数。

这是我能想到的最简单的参数:

call_user_func_array('getContent',array_merge(array(
    "article",
    "display:list",
    "find_series:team",
    "find_category:executive-team",
    "order:series",
    "find_parent_category:team",
    ),$myStyle)
);
编辑:Yazmat的解决方案可能会更好。
您可以将其与数量可变的参数一起使用,如下所示:

<? $myStyle = 
    "show:<div class='box box_stories'>",
    "show:<div class='box_pad box_img bwWrapper'>",
    "show:<div class='box_inner_pad'>",
    "show:<h3><a href='/staff-detail/__slug__'>__authorX__</a></h3>",
    "show:<p class='teamsummary'>__summary__</p>",
    "show:<p class='phone_icon'>__authorXworkphone__</p>",
    "show:</div>",
    "show:</div>",
    "show:</div>";

getContent(
    "article",
    "display:list",
    "find_series:team",
    "find_category:leadership-team",
    "order:series",
    "find_parent_category:team",
    $myStyle
);

getContent(
    "article",
    "display:list",
    "find_series:team",
    "find_category:executive-team",
    "order:series",
    "find_parent_category:team",
    $myStyle
); ?>
<?php
function myGetContent() {
    $defaults = array(
        "show:<div class='box box_stories'>",
        "show:<div class='box_pad box_img bwWrapper'>",
        "show:<div class='box_inner_pad'>",
        "show:<h3><a href='/staff-detail/__slug__'>__authorX__</a></h3>",
        "show:<p class='teamsummary'>__summary__</p>",
        "show:<p class='phone_icon'>__authorXworkphone__</p>",
        "show:</div>",
        "show:</div>",
        "show:</div>"
    );
    return call_user_func_array('getContent',array_merge(func_get_args(), $defaults));
}

您可以创建自己的函数,该函数只接受所需的参数,并使用这些参数和默认参数调用getContent()函数。并使用此新函数代替getContent()

编辑:

你可以这样做:

<?php
function myGetContent($param1,$param2,$param3,$param4,$param5,$param6){
  return getContent(
    $param1,
    $param2,
    $param3,
    $param4,
    $param5,
    $param6,
    "show:<div class='box box_stories'>",
    "show:<div class='box_pad box_img bwWrapper'>",
    "show:<div class='box_inner_pad'>",
    "show:<h3><a href='/staff-detail/__slug__'>__authorX__</a></h3>",
    "show:<p class='teamsummary'>__summary__</p>",
    "show:<p class='phone_icon'>__authorXworkphone__</p>",
    "show:</div>",
    "show:</div>",
    "show:</div>"
  );
}
你需要这个吗

<?php

getContent(
    array(
        'type'                  => 'article',
        'display'               => 'list',
        'find_series'           => 'team',
        'find_category'         => 'leadership-team',
        'order'                 => 'series',
        'find_parent_category'  => 'team'
    ),
    array(
        '<div class="box box_stories">',
        '<div class="box_pad box_img bwWrapper">',
        '<div class="box_inner_pad">',
        '<h3><a href="/staff-detail/__slug__">__authorX__</a></h3>',
        '<p class="teamsummary">__summary__</p>',
        '<p class="phone_icon">__authorXworkphone__</p>',
        '</div>',
        '</div>',
        '</div>'
    )
);


function getContent($argv, $shows){
    echo '<p>'.$argv['type'].'</p>';
    echo '<p>'.$argv['list'].'</p>';
    /* ... etc ... */

    echo '<pre>'.htmlspecialchars(print_r($argv, true), ENT_QUOTES).'</pre>';
    echo '<pre>'.htmlspecialchars(print_r($shows, true), ENT_QUOTES).'</pre>';
}

你能再详细一点吗?我不确定如何将标准参数传递给
getContent()
函数?似乎我也面临同样的问题。@Yazmat您可能想用
func\u get\u args()
来概括这一点。有人可能会这样复制它:幸运的是,我无法更改
getContent
函数本身。它是CMS的核心部分。我可以用一个虚拟的
getContent
函数(它只是var_转储它的输入)像这样运行它。您是否收到任何错误、警告。。或者函数什么都不做?
<?php

getContent(
    array(
        'type'                  => 'article',
        'display'               => 'list',
        'find_series'           => 'team',
        'find_category'         => 'leadership-team',
        'order'                 => 'series',
        'find_parent_category'  => 'team'
    ),
    array(
        '<div class="box box_stories">',
        '<div class="box_pad box_img bwWrapper">',
        '<div class="box_inner_pad">',
        '<h3><a href="/staff-detail/__slug__">__authorX__</a></h3>',
        '<p class="teamsummary">__summary__</p>',
        '<p class="phone_icon">__authorXworkphone__</p>',
        '</div>',
        '</div>',
        '</div>'
    )
);


function getContent($argv, $shows){
    echo '<p>'.$argv['type'].'</p>';
    echo '<p>'.$argv['list'].'</p>';
    /* ... etc ... */

    echo '<pre>'.htmlspecialchars(print_r($argv, true), ENT_QUOTES).'</pre>';
    echo '<pre>'.htmlspecialchars(print_r($shows, true), ENT_QUOTES).'</pre>';
}