shshaw
shshaw

Reputation: 3213

Combine PHP Function Arguments into a String

I have several calls to a getContent() function with arguments that are pulled into the function using func_get_args(). Several of the arguments stay the same, and I would like to set them as a variable to prevent having to duplicate the repeated arguments throughout all of these function calls.

Here is an example of the function call with the full arguments:

<? 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>"
); 
?>

Ideally, I would be able to do something like this:

<? $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
); ?>

Is this possible? What is the best way to approach this?

Note: I cannot change the getContent() function, it's a core function of the CMS

Upvotes: 1

Views: 133

Answers (4)

Vatev
Vatev

Reputation: 7590

This is the simplest one i can think of:

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)
);

Edit: Yazmat's solution will probably be better.
You can use it with a variable number of arguments like this:

<?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));
}

Upvotes: 1

e-info128
e-info128

Reputation: 4062

you need this?

<?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>';
}

Upvotes: 0

Oussama Jilal
Oussama Jilal

Reputation: 7739

You can create you own function that only takes the arguments you need and that calls the getContent() function with those arguments plus the defaults one. and use this new function instead of getContent()

Edit :

You do something like this :

<?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>"
  );
}

and then you call :

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

Upvotes: 1

SomeKittens
SomeKittens

Reputation: 39512

Unfortunately, this is not possible. What you have now is passing article, display, etc and $myStyle (which should be an array). You'll get errors because you didn't pass enough arguments.

If you can, check and see if getContent() has default parameters.

Upvotes: 1

Related Questions