2
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.

6 Answers 6

21

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));
Sign up to request clarification or add additional context in comments.

7 Comments

This is the way I'd do it, personally.
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.
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.
I probably would do it this way too
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.
|
8

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.

1 Comment

+1 for an excellent contribution and for pointing out that an array is impossible to avoid in this case.
3

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

2 Comments

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.
You could make this more efficient by using the "count($args)" only once at the beginning of the function and not in every repetition.
2
$a="hi";
$b = " world";
echo $a.$b;

3 Comments

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.
With some foreach statement, it shouldn't be too hard.
foreach would imply an array, which my question said I initially want to avoid :)
0

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

2 Comments

Thanks, David. I'll +1 you for that :)
According to the PHP manual, join is an alias of implode(), see hu.php.net/join
-1

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.