Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Simple script change help needed for novice

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Simple script change help needed for novice

Postby simplydave » Wed Jun 12, 2013 1:14 am

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 need it to be First Name "Mr Dave" and Last Name "Simply"

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 ;
}
}
}
simplydave
New php-forum User
New php-forum User
 
Posts: 1
Joined: Tue Jun 11, 2013 6:12 am

Re: Simple script change help needed for novice

Postby developerahusain » Wed Jun 12, 2013 5:13 am

do it like this.
<?
$text_line = explode(" ","Dave simply");
$firstname=$text_line [0];
$lastname=$text_line [1];

echo $firstname."&nbsp;".$lastname;

?>
developerahusain
developerahusain
New php-forum User
New php-forum User
 
Posts: 44
Joined: Tue May 28, 2013 10:04 pm
Location: India,Lucknow


Return to PHP coding => General

Who is online

Users browsing this forum: landi, MrExeqtor and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.