Jump to content
Killersites Forums

PHP Question - Join function?


Recommended Posts

I started learning PHP yesterday with the help of Stefan's videos, so I'm a newb and will be posting lots of questions in the next few weeks. Most of them will be easy to answer--just seems like a waste of time to search all day for an elementary answer when I could just post something. Bear with me.



I have an HTML form with text boxes. I know how to print the text received in the form. For instance, if I have 2 boxes--"First Name" and "Last Name"...


print $_POST['name_first'];

print $_POST['name_last'];


will render

Mary Jane Smith

(if the First Name value is "Mary Jane" and the Last Name value is "Smith")



I want to do something slightly more complicated. I want to

1) add a "+" between each word in a given text box (I couldn't figure out how to use the Join function to do this)

2) add specific text before and after the words in each text box, but ONLY if that text box contains contents.



Going back to the example above, I want to get the result:



What's the best way to do this? Thanks.

Link to comment
Share on other sites

Assuming I understand you correctly...


You could get the result you are looking for by doing something like this:


// get your info from the form
$firstname = $_POST['name_first'];
$lastname = $_POST['name_last'];

// double check that at least one of the two fields are filled out before continuing
if ($firstname !="" || $lastname !="")
 // convert all spaces within the strings to "+"
 $firstname = str_replace(" ", "+", $firstname);
 $lastname = str_replace(" ", "+", $lastname);

 //generate the results
 // checks if both firstname and last name are not blank
 if ($firstname != "" && $lastname != "")
   $string = "fn=" . $firstname . "&ln=" . $lastname;
 // else (if one of them is blank), check to see if firstname is blank, and only use last name
 else if ($firstname == "")
   $string = "ln=" . $lastname;
 // otherwise, lastname must be blank, so only use first name
   $string = "fn=" . $firstname;


I believe that should work. Try testing this and let me know if it does what you want or if you run into issues.

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...