Working with .doc files, file_get_contents and str_replace

  • guizza
  • Born
  • Born
  • guizza
  • Posts: 1

Post 3+ Months Ago

Well

I was working on a project where I need to fetch an existing word document (.doc) filled with place holders, replace with data from database, and give the result word file to the final user.

The catch is, I need to give a .doc (neither .rtf, nor .pdf or anything else).

I'm having some problem to do the replace. If I change the placeholder values with new ones, I get an error when the word loads, telling "The document name or path is not valid ...".

Image

PHP Code: [ Select ]
<?
ob_start();
$body = file_get_contents('files/template_document.doc');
$body = str_replace("CITY_COUNTRY", "Paris Italy", $body); // gives error
//$body = str_replace("CITY_COUNTRY", "CITY_COUNTRY", $body); // works
//$body = str_replace("CITY_COUNTRY", "CITY COUNTRY", $body); // works
echo $body;
       
$file_doc = 'Mail_'.time().'.doc';
header("Content-type: application/msword");
header("Content-Disposition: attachment; filename=$file_doc");
       
ob_end_flush();
?>
  1. <?
  2. ob_start();
  3. $body = file_get_contents('files/template_document.doc');
  4. $body = str_replace("CITY_COUNTRY", "Paris Italy", $body); // gives error
  5. //$body = str_replace("CITY_COUNTRY", "CITY_COUNTRY", $body); // works
  6. //$body = str_replace("CITY_COUNTRY", "CITY COUNTRY", $body); // works
  7. echo $body;
  8.        
  9. $file_doc = 'Mail_'.time().'.doc';
  10. header("Content-type: application/msword");
  11. header("Content-Disposition: attachment; filename=$file_doc");
  12.        
  13. ob_end_flush();
  14. ?>


Any help, where the problem lies?
Thanks guys.

************************************************************

EDIT: Just found, the str_replace throws error when the length of the replacing string is different than the original one. When the length of the replacing string is the same as the original one, everything works fine.

Means, php can replace the placeholder PHP, but ph or phpy can't replace the placeholder PHP.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Means, php can replace the placeholder PHP, but ph or phpy can't replace the placeholder PHP.

? This sounds strange to me. Just run this and see what i mean...

Code: [ Select ]
$data = 'Ozzu has a user called May';
$data = str_replace('May', 'Ozzu', $data);
echo "$data";
  1. $data = 'Ozzu has a user called May';
  2. $data = str_replace('May', 'Ozzu', $data);
  3. echo "$data";


The output i get is...

Code: [ Select ]
Debugger Output...

Content-type: text/html
Ozzu has a user called Ozzu
  1. Debugger Output...
  2. Content-type: text/html
  3. Ozzu has a user called Ozzu

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 84 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.