Jump to content


Photo

Remove special charactars before insert into database


  • Please log in to reply
1 reply to this topic

#1 lilywong

lilywong
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 27 April 2006 - 12:20 PM

I have currently write an php page to import the data from .txt file and insert into mysql database.

Data example in (test.txt file):
abc@hotmail.com
lily123@hotmail.com
110007;"mici@tm.net.my"
xyz@hotmail.com

the php page
<?
$fd = fopen ("c:\test.txt", "r"); 
if (!$fd) 
{ 
echo "ERROR: $errno - $errstr";
} 
else 
{ 
  while ( !feof($fd) ) 
 {  
    $buffer = fgets($fd, 4096); 
    <<i think i need to put checking here.... any ideas.. thanks...?       
    $query = " INSERT INTO emailAddress(Email) values ($buffer)"; 
 } 
fclose($fd); 
} 
?>
after in run the php script, the data will be insert into table and become:
ID Email
1 abc@hotmail.com
2 lily123@hotmail.com
3 110007;"mici@tm.com"--> want this to be mici@tm.com
4 xyz@hotmail.com

i want to remove the 110007; and the double quote, as i only want the take valid email address and save into my table. Is there any way to remove the 110007, ; , and "" so that the email address for ID 3 will become mici@tm.com ? Thanks

#2 bbaker

bbaker
  • Members
  • PipPipPip
  • Advanced Member
  • 127 posts
  • LocationNY

Posted 27 April 2006 - 12:55 PM

will other e-mails have the same syntax as 110007;"mici@tm.net.my"? or could it be something different?

if they will only have a syntax like the example the following function would work

function clean_email($email){

    if (strstr($email, ';') == TRUE)
    {
        $email = split(';',$email); //splits string at; $email[0] will be '110007'  $email[1] will be "mici@tm.net.my"
        $email = str_replace('"','',$email[1]); //removes " from $email[1]
    }

  return $email; // returns mici@tm.net.my
}

to use the function

$email = '110007;"mici@tm.net.my"';
echo clean_email($email); //returns mici@tm.net.my





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users