Need Function -- Will Pay

by 12 replies
14
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.
#programming #function #pay
  • 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
    • [ 1 ] Thanks
  • Thanks Chris. Good points all.
  • 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
  • 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 :-)
  • 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.
    • [1] reply
    • 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.
  • 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 :-)
  • 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!
    • [1] reply
    • 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.
      • [1] reply

Next Topics on Trending Feed