views:

302

answers:

6
join_strings(string $glue, string $var, string $var2 [, string $...]);

I am looking for something that would behave similar to the function I conceptualized above. A functional example would be:

$title = "Mr.";
$fname = "Jonathan";
$lname = "Sampson";

print join_strings(" ", $title, $fname, $lname); // Mr. Jonathan Sampson

After giving the documentation a quick look-over, I didn't see anything that does this. The closest I can think of is implode(), which operates on arrays - so I would have to first add the strings into an array, and then implode.

Is there already a method that exists to accomplish this, or would I need to author one from scratch?

Note: I'm familiar with concatenation (.), and building-concatenation (.=). I'm not wanting to do that (that would take place within the function). My intentions are to write the $glue variable only once. Not several times with each concatenation.

+1  A: 
$a="hi";
$b = " world";
echo $a.$b;
Ilya Biryukov
I'm familiar with concatenation. I don't want to have to repeat the $glue over and over, which is why I'm looking for a function.
Jonathan Sampson
With some foreach statement, it shouldn't be too hard.
luiscubal
foreach would imply an array, which my question said I initially want to avoid :)
Jonathan Sampson
+18  A: 

you can use join or implode, both do same thing and as you say it needs to be an array which is not difficult

join($glue, array($va1, $var2, $var3));
bumperbox
This is the way I'd do it, personally.
htw
I had thought about this (as noted in my question), but was wondering if there are already options that allow me to immediately add my strings rather than creating an array, and then adding strings.
Jonathan Sampson
I'm upvoting even though it's not the answer I am looking for. This is definitely a fast solution, that is both clean and elegant. It may be my fall-back.
Jonathan Sampson
I probably would do it this way too
Pascal MARTIN
You could have your custom string_join function add the strings to an array for you. All you would have to do is pass the individual strings as arguments.
Peter Spain
@Jonathan What is it about this solution that you don't like?
troelskn
@troelskn It's not that I don't like the solution. I was curious if there was a solution out there that wouldn't require me to use an array. If there's not, that's fine. I have nothing against this solution. I've really only avoided accepting it out of curiosity for other solutions that don't use an array.
Jonathan Sampson
A: 

You'll have to use the dot "." operator. This comes from abstract algebra theory, the strings being a monoid with respect to the concatenation operator, and the fact that when dealing with algebraic structures the dot is usually used as a generic operator (the other being "+", which you may have seen used in other languages).

For quick reference, being a monoid implies associativity of the operation, and that there exists an identity element (the empty string, in our case).

Agos
He doesn't want to use the concatenation operator directly. He was asking if there already existed a function to combine strings to save him the effort of manually coding the concatenation over and over.
Peter Spain
+3  A: 

Try this:

function join_strings($glue, $arg){
   $args = func_get_args();
   $result = "";
   $argcount = count($args)
   for($i = 1; $i < $argcount; $i++){
       $result .= $args[$i];
       if($i+1!=count($args){
           $result .= $glue;
       }
   }
   return $result;
}

EDIT: Improved function thanks to comment suggestion

luiscubal
I wrote up a similar function just minutes ago. Plus 1 for arriving to the same conclusion. I am going to go with the accepted-solution using join() instead as it's more parsimonious. Thank you for your involvement.
Jonathan Sampson
You could make this more efficient by using the "count($args)" only once at the beginning of the function and not in every repetition.
Miroslav Solanka
+7  A: 

You can use func_get_args() to make implode() (or its alias join()) bend to your will:

function join_strings($glue) {
    $args = func_get_args();
    array_shift($args);
    return implode($glue, $args);
}

As the documentation for func_get_args() notes, however:

Returns an array in which each element is a copy of the corresponding member of the current user-defined function's argument list.

So you still end up making an array out of the arguments and then passing it on, except now you're letting PHP take care of that for you.

Do you have a more convincing example than the one in your question to justify not simply using implode() directly?

The only thing you're doing now is saving yourself the trouble to type array() around the variables, but that's actually shorter than the _strings you appended to the function name.

mercator
+1 for an excellent contribution and for pointing out that an array is impossible to avoid in this case.
Jonathan Sampson
A: 

While I agree with the accepted answer, see this article thats says implode is faster than join.

David Archer
Thanks, David. I'll +1 you for that :)
Jonathan Sampson
According to the PHP manual, `join` is an alias of `implode()`, see http://hu.php.net/join
Török Gábor