/    Sign up×
Community /Pin to ProfileBookmark

Very strange problem with STR_REPLACE

Hello people
I am posting a question about something that is really puzzling to me.
I am using the [B][COLOR=”Red”]exact same code [/COLOR][/B]in 3 different pages. [COLOR=”red”][B]It only works on the first page[/B][/COLOR]!!!
What am I doing wrong?

I send variables ([B]D1, D2, D3,…[/B]) to a file and replace items ([B]D1P,D2P,D3P,…[/B]) ith those variables.
My code works perfectly in one page but as I mentioned I repeat the same procedure with the exact same code (just different file name) and the second page does not work. [COLOR=”red”][B]The page opens but variables do not get replaced.[/B][/COLOR]

The code is as:

[code=php][/[COLOR=”RoyalBlue”]<?php
header( “Content-Type: application/msword” );
header( “Content-Disposition: inline, filename=GeneralDes.rtf”);

// open our template file
$filename = “T_TD.rtf”;
$fp = fopen ( $filename, “r” );

//read our template into a variable
$output = fread( $fp, filesize( $filename2 ) );

//REPLACING THE ITEMS
$D1=’aaaa’;
$D2=’bbbb’;
$D3=’cccc’;

$output=str_replace(“<<D1P>>”,$D1,$output);
$output=str_replace(“<<D2P>>”,$D2,$output);
$output=str_replace(“<<D3P>>”,$D3,$output);
echo $output;
fclose ( $fp );

?>[/COLOR]PHP]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 06.2007 — I don't see anything obviously wrong. First thing I would do at this point is view the RTF template file in a plain text editor, and make sure there is nothing breaking up your search string (such as RTF mark-up or newlines/carriage-returns).
Copy linkTweet thisAlerts:
@XTIauthorNov 06.2007 — NogDog that template is formated variables fill different positions in the document. But the exact same thing was happening successfully in the first set which works.

Also at least the very first item D1P should work but even that one does n't.

I have tried changing the template entirely, or formating it differently or formating the variables after they are filled in [B](Bold, Italic, color, etc..) [/B]bt nothing has helped so far.
Copy linkTweet thisAlerts:
@MrCoderNov 07.2007 — Is your browser tricking you by caching the files?

Try replacing the..
[code=php]
header( "Content-Disposition: inline, filename=GeneralDes.rtf");
[/code]

..with a different file name every time?
Copy linkTweet thisAlerts:
@XTIauthorNov 09.2007 — I have tested every possible source of problem (including renaming files and variables). [B][COLOR="Red"]I found out that problem get resolved when I copy the placeholders name [COLOR="Navy"]<<B1P>> [/COLOR] from PHP file and paste it exactly as it is in the RTF file instead of typing it[/COLOR][/B]!!!

It seems like [B]when I use MS Word to create the RTF template, it adds additional code into the placeholder names[/B], which casue the problem for PHP file to find and replace them with variable values.

Any idea as why this is, and how can it be prevented?

There are almost 100 placeholders such as [COLOR="navy"]<<B1P>> [/COLOR]in that RTF file and it is ridiculus that one has to copy and paste them one by one from PHP file instead of simply typing them in Word.

Any suggestions is greatly appreciated.
Copy linkTweet thisAlerts:
@NogDogNov 09.2007 — As I suggested earlier, try opening the RTF files in a plain text editor instead of a WYSIWYG editor (such as Word or Wordpad). Then find some of your place-holders in the text editor and see if there is a pattern to how they are marked up. Then you can either do a search-and-replace within the editor to change the place-holders, or else change your PHP search strings to match the RTF mark-up.
×

Success!

Help @XTI spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...