SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Don't eat yellow snow spaceman's Avatar
    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    1,039
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Text replace in Word doc on linux server?

    Hi All,

    Is it possible to perform text search / find and replace operations on a Word doc on a linux server?

    I've got a potential client who wants a document management system, but with the ability to search and replace text - bit like a mail merge system.

    So in lots of Word docs that the client has uploaded there be some text like:

    "Hello, my name is [fullname]."

    Members of my client's site will have to login. Their member details will include their fullname. So if a logged in member selects a particular Word document to view, the system will, on demand, auto-insert the member's fullname into the document.

    Naturally if the documents were plain text the answer to this question would be 'too easy'. For this to work, it has to be 100% reliable with Word docs. Or perhaps the answer is 'too hard until Office incl. Word goes XML'?

    Possible? Here's hoping. Continuing my own research...
    Web Design Perth Melbourne .:. Itomic Business Website Solutions
    Drupal Experts .:. Drupalise

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    Don't eat yellow snow spaceman's Avatar
    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    1,039
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice. Thanks for the link. More research to do now. :-)

    p.s. Looks risky: http://support.microsoft.com/default...;EN-US;q257757
    Web Design Perth Melbourne .:. Itomic Business Website Solutions
    Drupal Experts .:. Drupalise

  4. #4
    Don't eat yellow snow spaceman's Avatar
    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    1,039
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've just had a better idea. It wouldn't be too much to ask of the client to save all documents in .xml format. Should be a damn sight easier to do a text search and replace on an .xml document than a .doc document.

    Comments anyone?
    Web Design Perth Melbourne .:. Itomic Business Website Solutions
    Drupal Experts .:. Drupalise

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spaceman
    Well, I haven't used it for anything serious except a bit of reading from excell sheets, so I couldn't say for sure. However, I would expect you to be able to at least read content out of a word document, whereas more advanced features might fail.

    Quote Originally Posted by spaceman
    It wouldn't be too much to ask of the client to save all documents in .xml format. Should be a damn sight easier to do a text search and replace on an .xml document than a .doc document.
    Certainly, if you have that power over your users. Still - the xml-output from word isn't exactly simple, so you'll have to do some work.

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2005
    Location
    Venlo, the Netherlands
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did something similar.
    When you save the wordfiles to the server in .rtf format, you can easily open them, replace certain tags and save it as .doc on a different location.
    The code looks something like this:

    PHP Code:
    // Open the file
    if(!$sRtfFile file_get_contents('file.rtf')){
      die(
    'template file could not be found');
    }

    // Input
    $aInject = array('value1''value2''etcetera');

    // Placeholders in the template
    $aInput =  array('[placeholder1]''[placeholder2]''[placeholder3]');

    // Replace the placeholders
    $sOutput str_replace($aInput$aInject$sRtfFile);

    // Save file to disk 
    file_put_contents(c:/path/filename.doc$sOutput);

    // Force a download for end user
    header('Content-Description: File Transfer');
    header('Content-Type: application/msword');
    header('Content-Disposition: attachment; filename = filename.doc');
    header('Cache-control: private');
    header('Pragma: no-cache');
    header('Expires: 0');

    echo 
    $sOutput
    Don't mess with the COM library. I did and it gave me nothing but trouble. And this was on a Windows server. I can't imagine the problems you'll face when using COM on a Linux server.

  7. #7
    Don't eat yellow snow spaceman's Avatar
    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    1,039
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Vennie, great suggestion. I'll research this further. Asking the client to save as rtf or xml format from Word is equally as easy - so it'll really come down to which is the easier format to work (on the server) with that will satisfy all or most of the customer's requirements.
    Web Design Perth Melbourne .:. Itomic Business Website Solutions
    Drupal Experts .:. Drupalise


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
  •