#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    How to parse a .txt file in php script?


    how to parse a .txt file in php script?
    the contents of the file. txt:

    ----------- cat -------------
    Nom_cat=cat1
    desc_cat=test description cat
    ----------- detail_cat ---------------
    det_nom=test nom
    det_desc=schema precis
    det_titre=le schema de l'appli
    ------------- sous_cat_prd -------------
    prd_nom=testprd,prd_descr=descrprd,prd_type=typeprd
    prd_nom=ordi,prd_descr=perform,prd_type=ibm
    prd_nom=apareil,prd_descr=description de l'apareil,prd_type=photo
    -------------- sous_cat_schema -------------
    scs_nom=schemaprd:scs_descr=descr schem:scs_adr=77 rue
    scs_nom=schema2:scs_descr=description du schema2:scs_adr=40 bldv

    ----------- cat -------------
    Nom_cat=cat2
    desc_cat=test2 description cat
    ----------- detail_cat ---------------
    det_nom=testnom2
    det_desc=schema precis test2
    det_titre=le schema de l'appli2
    ------------- sous_cat_prd -------------
    prd_nom=testprd2,prd_descr=descr prd,prd_type=type produit
    prd_nom=machine,prd_descr=performant,prd_type=nespress
    -------------- sous_cat_schema -------------
    scs_nom=scc:scs_descr=descr schem:scs_adr=33 avn
    scs_nom=vers:scs_descr=description du vers:scs_adr=30 bldv

    I want return of the proofing fields and their values ​​to three insertion:
    insert into cat (Nom_cat,desc_cat,det_nom,det_desc,det_titre) values ($Nom_cat,$desc_cat,$det_nom,$det_desc,$det_titre)
    insert into sous_cat_prd (Nom_cat,prd_nom,prd_descr,prd_type) values ($Nom_cat,$prd_nom,$prd_descr,$prd_type)
    insert into sous_cat_schema (Nom_cat,scs_nom,scs_descr,scs_adr) values ($Nom_cat,$Nom_cat,$scs_nom,$scs_descr,$scs_adr)
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,690
    Rep Power
    1958
    Do you know how to read the file and get each line?
    First you should check if the line start with a - and then extract the "middle text"
    Secondly, you should validate if the lines contains a = which can be done using a function like [phpnet=strpos]strpos()[/phpnet].

    You should be able to use a function like split or explode to separate the key and value.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Posts
    40
    Rep Power
    9
    You can also use this to explode the file line by line
    $filecat=file_get_contents("your-file.php");
    $d=explode("\n",$filecat);
    //to display the content use
    foreach($filecat as $catline){

    //your mysql conditional insert can be here
    echo $catline;

    }
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,179
    Rep Power
    9398
    Originally Posted by prince55l
    You can also use this to explode the file line by line
    $filecat=file_get_contents("your-file.php");
    $d=explode("\n",$filecat);
    Or just use file.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,008
    Rep Power
    376
    please dont use file if your txt file is TOO big otherwise php will hang due to timeout issues

IMN logo majestic logo threadwatch logo seochat tools logo