14

I need to concatenate an indeterminate number of strings, and I would like a space in between two adjoining strings. Like so a b c d e f.

Also I do not want any leading or trailing spaces, what is the best way to do this in PHP?

7
  • How do you store those strings? Are they in an array? Or in separate variables? Commented Nov 9, 2011 at 16:50
  • They are separate variables, and if the vriable is not set or an empty string I do not want it included in the concatenated string. Commented Nov 9, 2011 at 16:55
  • @freshest And how are the variables generated? Can't you put the values in an array instead of free variables? Commented Nov 9, 2011 at 16:58
  • 1
    1- for the "best" in the title Commented Nov 9, 2011 at 17:01
  • 1
    Then just build the array of strings as you iterate the database rows and put the variables in the array only if they're not empty (then you can do the implode). Add that code here if you're unsure how to do it. Commented Nov 9, 2011 at 17:01

6 Answers 6

37

You mean $str = implode(' ', array('a', 'b', 'c', 'd', 'e', 'f'));?

Sign up to request clarification or add additional context in comments.

Comments

6

Simple way is:

$string="hello" . " " . "world";

Comments

4
function concatenate()
{
    $return = array();
    $numargs = func_num_args();
    $arg_list = func_get_args();
    for($i = 0; $i < $numargs; $i++)
    {
        if(empty($arg_list[$i])) continue;
        $return[] = trim($arg_list[$i]);
    }
    return implode(' ', $return);
}

echo concatenate("Mark ", " as ", " correct");

Comments

4
$strings = array( " asd " , NULL, "", " dasd ", "Dasd  ", "", "", NULL );

function isValid($v){
return empty($v) || !$v ? false : true;
}

$concatenated = trim( implode( " ", array_map( "trim", array_filter( $strings, "isValid" ) ) ) );

//"asd dasd Dasd"

Comments

4

I just want to add to deviousdodo's answer that if there is a case that there are empty strings in the array and you don't want these to appear in the concatenated string, such as "a,b,,d,,f" then it will better to use the following:

$str = implode(',', array_filter(array('a', 'b', '', 'd', '', 'f')));

Comments

2

considering that you have all these strings collected into an array, a way to do it could be trough a foreach sentence like:

$res = "";
foreach($strings as $str) {
   $res.= $str." ";
}

if(strlen($res > 0))
    $res = substr($res,-1);

in this way you can have control over the process for future changes.

4 Comments

Sounds overkill when you have implode().
yay, I know, but I usually like to have things opened and under control that gives me the freedom to make changes anytime when I'm facing unknown datatypes, cuz now it's an array of string but you never know what comes during the developing, so I like to have that kind of freedom. I always have time to change it to a simple implode / join as far as I'm sure about stuff.
Your solution (usage of foreach) assumes the strings are in an array. Usage of implode would also assume the strings are in an array. I don't see the freedom here, I only see a bunch of unnecessary lines which decrease readability.
that gives me the freedom to have a string of whatever objs and change the interiors of the statements in the loop, that's it. and this is just for prototyping issues, of course when u r sure, u get it right, so if it'll be an array of string obviously u choose a more fancy and less consuming solution like implode or join

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.