Jump to content

Split string to capture first name ?


ttmt

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.