simplydave Posted June 11, 2013 Share Posted June 11, 2013 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. ExampleName - "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. Quote Link to comment https://forums.phpfreaks.com/topic/279038-text-field-name-split-for-prestashop-import/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.