ttmt Posted June 4, 2009 Share Posted June 4, 2009 Hi all I have a simple email script that shows a message when the email has been sent. The message uses the name used in the email form $message = "Thank you $name_field, we will be in touch soon."; This name could be two names - first name and last last name. Is it possible to split this string and just use the first name. I was thinking it might be possible to use the space character to determine the split. <?php $message = ""; if(isset($_POST['submit'])) { $errors = array(); $required_fields = array('name','email','message'); foreach($required_fields as $fieldname){ if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])){ $errors[] = $fieldname; } } if(empty($errors)){ $to = "[email protected]"; $name_field = $_POST['name']; $email_field = $_POST['email']; $subject = "Email from Website"; $message = $_POST['message']; // $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message"; // mail($to, $subject, $body); $message = "Thank you $name_field, we will be in touch soon."; }else{ $message = "Please complete all fields."; } } ?> Link to comment https://forums.phpfreaks.com/topic/160895-split-string-to-capture-first-name/ Share on other sites More sharing options...
Dtonlinegames Posted June 4, 2009 Share Posted June 4, 2009 <?php $name =explode(' ', $name_field); $firstname =$name[0]; $lastname=$name[1]; ?> Link to comment https://forums.phpfreaks.com/topic/160895-split-string-to-capture-first-name/#findComment-849103 Share on other sites More sharing options...
Ken2k7 Posted June 4, 2009 Share Posted June 4, 2009 Use trim on $name_field as well. Link to comment https://forums.phpfreaks.com/topic/160895-split-string-to-capture-first-name/#findComment-849222 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.