string concatenate function

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

  1. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice