SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    0 Post(s)
    0 Thread(s)

    how to upload this file..?

    hello guys

    i have asimple problem

    i have file a txt file with 5000 records similar to this.

    abase v. To lower in position, estimation, or the like; degrade
    abbess n. The lady superior of a nunnery
    abbey n. The group of buildings which collectively form the dwelling-place
    abbot n. The superior of a community of monks
    abdicate v. To give up (royal power or the like)
    abdomen n. In mammals, the visceral cavity between the diaphragm

    now i need to load these data in a database..

    how do write a parser whcih can read each line and load each line in a database

    i know a script how ever, i need to add x[] ( a variable name to each which can behectic.

    please help me out

  2. #2
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Cornwall, UK
    0 Post(s)
    0 Thread(s)
    start by splitting into lines:
    PHP Code:
    $data "
    abase v. To lower in position, estimation..
    $lines explode("\r\n"$data); 
    then go through each line and try to make sense of it using regular expressions:
    PHP Code:
    foreach($lines as $line) {
    preg_match("^([a-z-]+) (v|n)\. (.*)$"$line$matches)) {
    /*the regex says "some letters or hyphens, a space, then a v or an n then a dot, then anything" if there are other types other than v or n then just add them separated by a vertical pipe */
    $word $matches[1];
    $type $matches[2];
    $definition $matches[3];
    /* now you've got your data you can do what you like with it, probably best to echo it out first couple of times to make sure that the pattern is matching it correctly,  then put a mysql statement here */
    mysql_query("INSERT INTO words (word, type, definition) VALUES ('".addslashes($word)."', '".addslashes($type)."', '".addslashes($definition)."')");
      } else { 
    //line didn't match pattern- maybe echo for manual investigation


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts