1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

string concatenate function

Discussion in 'PHP' started by pein87, Aug 14, 2010.

  1. pein87

    pein87 New Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    0
    Occupation:
    Web Dev
    Location:
    Limbo
    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:
    <?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);

    ?>
     
    shabbir likes this.

Share This Page