Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   string concatenate function (http://www.go4expert.com/forums/string-concatenate-function-t23009/)

pein87 14Aug2010 10:41

string concatenate function
 
In C and C++ theres a strcat() function that joins strings, php has a well working function that turns an array into a string with a separator. I made a simple function called str_join() that uses that function to turn an array into a string and print it to the screen. Thinking of adding the separator as an argument. Also shows how to use it in two examples, let me know how it works for you and use as you please but if you mod the cod e please share it with the community and I'll update the code as I make changes to it.


PHP Code:

<?php

function str_join($text)
{
if (
is_array($text)) //check if its an array or not
{
    
$joined_txt implode(" ",$text); // turn array into a space seperated string
    
return $joined_txt;
}
else
{
    echo 
"the first argument is invalid, expecting an array."// trigger error
}
}

$arr = array("pein87""PEIN87","Pein87");
echo 
str_join(array("pein87""Charly""Gibs"));
echo 
" ";
echo 
str_join($arr);

?>



All times are GMT +5.5. The time now is 21:58.