Jump to content

Text field Name split for Prestashop import


simplydave

Recommended Posts

I am new to PHP but have been given some code to split a single text field "Name" in to two seperate fields "First name" and "Last Name" so I can import the data in to my Prestashop v1.4.7.3 website.

 

The script works but the problem I have is that it splits the "Name" field the wrong way around.

Example
Name - "Mr Dave Simply" is split in to First Name "Simply" and Last Name "Mr Dave"

 

I assume I can just change the results around but I can not figure out how, so am asking for some advise.

 

I have a class.php

 

      public static function getName($fullname)
      {
          $result = array() ;
          $result['company'] = '' ;
          if ( preg_match('/,|\//', $fullname) )
          {
            $parts = preg_split('/,|\//', $fullname) ;
            $var = trim(substr($parts[0], 0, 31)) ;
            $result['company'] = trim(substr($parts[1], 0, 31)) ;
          }
          else  $var = substr($fullname, 0, 31) ;
          $var = preg_replace('/[0-9!<>,;?=+()@#"�{}_$%:]/', '', $var) ;
          $var = trim($var) ;
          $var = substr($var, 0, 31) ;
          $var1 =  explode(' ', $var) ;
          $sz = sizeof($var1) - 1 ;
          $lastname = $var1[$sz] ;
          unset($var1[$sz]) ;
          $firstname = implode(' ', $var1) ;
          $firstname = empty($firstname) ? 'unknown' : $firstname ;
          $lastname = empty($lastname) ? 'unknown' : $lastname ;
          $result['firstname'] = ucfirst($firstname) ;
          $result['lastname'] = ucfirst($lastname) ;
          return($result) ;
      }

 

and an import.php

 

          if ( $account_type == 2 && isset($order->BuyerEmail) )
          {
            $email_address = (string)$order->BuyerEmail ;
            $customer = new Customer() ;
            if ( empty($email_address ) )
            {
              echo basename(__FILE__) . ': ' . __LINE__ . ' - ' . $this->l('Couldn\'t add this customer') . ' : ' . $name . '(' . $email_address . ')' ;
              continue ;
            }
            $customer->getByEmail( $email_address ) ;
            if ( $customer->id )
            {
                $id_customer = $customer->id ;
            }
            else
            {
                $email_address = (string)$order->BuyerEmail ;
                $name = Amazon_Address::getAmazonName( (string)$order->Address->Name ) ;
                $customer->firstname = $name['firstname'] ;
                $customer->lastname = $name['lastname'] ;
                $customer->email = $email_address ;
                $customer->passwd = md5(rand());
                if ( ! $customer->add() )
                {
                    echo $this->l('Couldn\'t add this customer') . ' : ' . $name . '(' . $email_address . ')' ;
                }
                else
                {
                    $id_customer = $customer->id ;
                }
            }
          }

 

Thanks for your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...

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.