Results 1 to 5 of 5

Thread: php to parse word document

  1. #1
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default php to parse word document

    hi,
    i need to parse word documents for name,email and contact information and insert the values to a database using php code.
    can someone please help me with this as i don't really know how to go about it.

    Thanks in advance ,
    Suk

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Your PHP would look something like:
    Code:
    <?php
    $file = fopen('word.doc','rb'); 
    while($line = fgetcsv($file, null, ":")){
      ${strtolower($line[0])} = $line[1]; 
    }
    echo $phone;
    ?>
    Then make a new file called word.doc:

    Code:
    Name: Bob Cammer
    Email: cammer.bob@idk.com
    Phone: 1.888.888.8888
    You can simply add anything to it:
    Code:
    Name: Bob Cammer
    Email: cammer.bob@idk.com
    Phone: 1.888.888.8888
    Type: Artwork
    To output it, you would then add this at the bottom of the php script(highlighted in the php script above):
    Code:
    echo $type; //output Artwork
    Notice how the type is capitalized in the word doc, but not in the php.
    Jeremy | jfein.net

  3. #3
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thnx for the reply, but i didnt really follow the code.
    i have a word doc called txtfile.doc in which i have say name and age it can have n number of details.
    what i want to do is extract the word which comes after say Name and then the numbers which come after Phone.
    How???

  4. #4
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    Your PHP would look something like:
    Code:
    <?php
    $file = fopen('word.doc','rb'); 
    while($line = fgetcsv($file, null, ":")){
      ${strtolower($line[0])} = $line[1]; 
    }
    echo $phone;
    ?>
    Then make a new file called word.doc:

    Code:
    Name: Bob Cammer
    Email: cammer.bob@idk.com
    Phone: 1.888.888.8888
    You can simply add anything to it:
    Code:
    Name: Bob Cammer
    Email: cammer.bob@idk.com
    Phone: 1.888.888.8888
    Type: Artwork
    To output it, you would then add this at the bottom of the php script(highlighted in the php script above):
    Code:
    echo $type; //output Artwork
    Notice how the type is capitalized in the word doc, but not in the php.
    You are parsing a csv file here. You cant use fgetcsv on a real .doc word file. Ofcourse you can make a csv file and call it word.doc but it isnt going to be a properly formatted MS word .doc file.

    Now that MS have made libraries available to the open source world so that things like open office can work with .doc files, I would have thought that someone would have ported this to a php api by now. From all the chatter around from people that want to know how to parse a .doc file, I guess its not be written yet. I would advise people to look for .doc to xml converters and to parse the xml in PHP as they need to. These are the open source xml standards written by microsoft.

    Thanks

  5. #5
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Guys, it doesn't work like that. You need to use some sort of library to read these files. Use an open source package like Antiword to read .doc files.
    http://www.winfield.demon.nl/
    - Josh

Bookmarks

Posting Permissions

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