Need Function -- Will Pay

by lisag
12 replies
I need a PHP, PERL or Python function that takes these parameters:

$file_to_open,$array_of_strings_to_search(), $array_of_strings_to_replace,$file_to_write

The function will open the specified RTF file, search for each occurrence of the strings in $array_of_strings_to_search() and replace them with the corresponding value in $array_of_strings_to_replace. Some search instances may exist multiple times in the document.

Upon completion that file is saved as a PDF with the name and location specified in $file_to_write.

Returns TRUE upon successful completion or FALSE upon failure.

PHP preferred.
#function #pay
  • Profile picture of the author Mr. Enthusiastic
    Hi Lisa,

    I can help you improve the design, although I don't think I've the time to complete implementation for you.

    Instead of two parameters for search and replace, each a one-dimension array, I would use an associative array. For each entry, the key is the search string, and the value is the replacement string. You could save this object and reuse it if you are processing multiple files.

    You need to define error handling for the input parameters. A null search string is an obvious error, but what about a null replacement string? Should that entry be skipped? Should an exception be thrown?

    In the Windows world, I would use Word to do the search & replace and export. In the php world, I imagine that OpenOffice Writer might be suitable. It can read rtf's, perform search & replace operations, and export as pdf. I think you need a third party utility here, because your text string might have some embedded formatting in the rtf. A smart rtf utility should be able to preserve the formatting while replacing the text, and again preserve the formatting for the export to pdf.

    A single FALSE on failure doesn't indicate where the failure occured. I recommend that the routine throw exceptions related to opening the source file; performing the search & replace; and exporting the pdf. This will make it possible to troubleshoot any runtime errors that occur.

    Chris
    {{ DiscussionBoard.errors[1579603].message }}
  • Profile picture of the author lisag
    Thanks Chris. Good points all.
    Signature

    -- Lisa G

    {{ DiscussionBoard.errors[1579735].message }}
  • Profile picture of the author mywebwork
    Hi Lisa

    Not sure if this is of any use to you, it's a PHP class that reads RTF files.

    PHP - Reading the clean text from RTF

    Thought you may be able to use it to read through the file, replace all the occurrences of your text string and then use another PHP class to produce the PDF output.

    Hope it is of some help.

    Bill
    {{ DiscussionBoard.errors[1579811].message }}
  • Profile picture of the author lisag
    Thanks Bill. I looked at that yesterday. It strips out all the formatting. I need the formatting intact. I am trying to build a PDF brander for a site I'm launching. The ones I've seen (and bought) suck. If you know one that sucks less, let me know :-)
    Signature

    -- Lisa G

    {{ DiscussionBoard.errors[1579859].message }}
  • Profile picture of the author phptechie
    Lisa,

    I'm a Freelance PHP MySQL developer with 6+ yrs of exp.

    I can help you with it , but the problem , you can't do it with just a single funtion.

    The function can read the RTF file , search the keywords & replace it with the given string , but in-order to create a PDF document output , you need to use PDF libraries like FPDF.

    Pls do let me know & we shall proceed further.
    {{ DiscussionBoard.errors[1582507].message }}
    • Profile picture of the author lisag
      Originally Posted by phptechie View Post

      Lisa,

      I'm a Freelance PHP MySQL developer with 6+ yrs of exp.

      I can help you with it , but the problem , you can't do it with just a single funtion.

      The function can read the RTF file , search the keywords & replace it with the given string , but in-order to create a PDF document output , you need to use PDF libraries like FPDF.

      Pls do let me know & we shall proceed further.
      Hi ,

      Yes, I realize that. I had envisioned a call to the pdf conversion function either inside the s&r function or after the return.

      I should point out that I'm perfectly happy to skip the RTF portion and deliver the s&r a completed PDF that needs to be updated, but that seemed like more of a challenge when I wwas looking for a way to do it myself.
      Signature

      -- Lisa G

      {{ DiscussionBoard.errors[1582529].message }}
  • Profile picture of the author phptechie
    So , If you still need this done , pls confirm it via PM or mail to c.sundar@gmail.com & I shall help you with it :-)
    {{ DiscussionBoard.errors[1593062].message }}
  • Profile picture of the author Richard Pickett
    Hey Lisa G,


    <?php

    function replaceStringsInFile( $fileIn, $arraySearch, $arrayReplace, $fileOut )
    {
    file_put_contents( $fileOut, str_replace( $arraySearch, $arrayReplace, file_get_contents( $fileIn ) ) );
    }

    replaceStringsInFile( 'ls', array( 'puppy', 'songs' ), array( 'kitty', 'poems' ), 'ls.mod' );

    ?>

    I had echoed ls into a file called ls and wanted to change "puppy" to "kitty" and "songs" to "poems" - it replaced every occurrence of each of those words and put the results in ls.mod

    Let me know if you need any more help.

    Thanks!
    Signature
    Mastering Email Delivery
    Have (or want) a large list and want to ensure delivery and eliminate spam reports?
    Inbox me
    {{ DiscussionBoard.errors[1596269].message }}
    • Profile picture of the author Mr. Enthusiastic
      Originally Posted by Richard Pickett View Post

      Hey Lisa G,


      <?php

      function replaceStringsInFile( , , , )
      {
      file_put_contents( , str_replace( , , file_get_contents( ) ) );
      }

      replaceStringsInFile( 'ls', array( 'puppy', 'songs' ), array( 'kitty', 'poems' ), 'ls.mod' );

      ?>

      I had echoed ls into a file called ls and wanted to change "puppy" to "kitty" and "songs" to "poems" - it replaced every occurrence of each of those words and put the results in ls.mod

      Let me know if you need any more help.

      Thanks!
      Richard, the problem is that she wants to search and replace the text while leaving rtf formatting intact, without having to type in rtf formatting codes as the search and replace strings. For example, if puppy is italic then kitty should also be italic, and if songs is bold and underlined then that's how poems should be formatted as well, without any further work on her part. This means she needs to use an rtf library with this functionality.
      {{ DiscussionBoard.errors[1596332].message }}
      • Profile picture of the author Richard Pickett
        Originally Posted by Mr. Enthusiastic View Post

        Richard, the problem is that she wants to search and replace the text while leaving rtf formatting intact, without having to type in rtf formatting codes as the search and replace strings. For example, if puppy is italic then kitty should also be italic, and if songs is bold and underlined then that's how poems should be formatted as well, without any further work on her part. This means she needs to use an rtf library with this functionality.
        I doubt it.

        RTF is all text-based, here's a sample:

        Code:
         {rtf1ansideff0
        {colortbl;red0green0blue0;red255green0blue0;}
        This line is the default colorline
        cf2
        This line is redline
        cf1
        This line is the default color
        }
        as you can see, the normal text words can easily be replace w/ search/replace functionality in php.

        Anyway, Lisa can try out my code, if it suites her she's good to go. My impression is she has rtf files with things like %WEBSITE_NAME% and wants to replace that "variable" with the real website name before converting to pdf.

        Maybe I'm reading too much into her post, but I still think the search/replace will do it.

        Let's see if it helps her.
        Signature
        Mastering Email Delivery
        Have (or want) a large list and want to ensure delivery and eliminate spam reports?
        Inbox me
        {{ DiscussionBoard.errors[1596350].message }}
        • Profile picture of the author Mr. Enthusiastic
          Originally Posted by Richard Pickett View Post

          as you can see, the normal text words can easily be replace w/ search/replace functionality in php.

          Anyway, Lisa can try out my code, if it suites her she's good to go.
          Aha, I didn't know that was how rtf formatting is encoded. If you're right, you not only sped Lisa on her way but provided a really useful tip for anyone else parsing rtf files. Thank you!
          {{ DiscussionBoard.errors[1596372].message }}
        • Profile picture of the author Voon
          Originally Posted by Richard Pickett View Post

          I doubt it.

          RTF is all text-based, here's a sample:

          Code:
           {rtf1ansideff0
          {colortbl;red0green0blue0;red255green0blue0;}
          This line is the default colorline
          cf2
          This line is redline
          cf1
          This line is the default color
          }
          as you can see, the normal text words can easily be replace w/ search/replace functionality in php.

          Anyway, Lisa can try out my code, if it suites her she's good to go. My impression is she has rtf files with things like %WEBSITE_NAME% and wants to replace that "variable" with the real website name before converting to pdf.

          Maybe I'm reading too much into her post, but I still think the search/replace will do it.

          Let's see if it helps her.
          I think it's depend on the editor and charset. Although it displays %WEBSITE_NAME% on the editor. When it is saved, it's not.

          One example is this (using Microsoft Word 2003):
          Code:
          {rtlchfcs1 af0afs20 ltrchfcs0 insrsid9193420charrsid8586488 hichaf0dbchaf13lochf0 %hichaf0dbchaf13lochf0 WEBSITE_NAME%}
          Another example I found using the hyperlink:
          Code:
          {rtlchfcs1 af2afs20 ltrchfcs0 f2fs20insrsid10953889 hichaf2dbchaf13lochf2 HYPERLINK "%25URL%25"}
          %URL% is not saved as %URL% in the RTF source.

          So, have to becareful dealing directly with the source.

          ... 2 cents
          Signature

          .

          {{ DiscussionBoard.errors[1597512].message }}

Trending Topics