2

My PHP code is generating 495 HTML pages from 495 txt files and working correctly. But right now, I'm trying to change it as a way to change the value of title tag dynamically; so I'm trying to replace %TITLE% with $Oneline that is the first line of each txt pages.

I have tried many syntaxes such as prg_replace, str_replace and much more all seems unsuccessful. In fact those lines of codes change nothing on my HTML pages.

To be more clear:

  1. Trying to replace %TITLE% with $Oneline.
  2. $Oneline is the first line of the txt file.

Thanks for any help.

<?php
for ($i = 1; $i <= 495; $i++)
{$j = 1;
$SousrceFile = @fopen($SousrceFile, 'r') ;
$TargetFile = fopen($TargetFile, 'w+') ;
fwrite($TargetFile, "<title>%TITLE%</title>\n");
    while ($Oneline = @fgets($SousrceFile, 4096)) 
    {$j = $j + 1;
        if (strlen($Oneline) !==0)
        {
        $title = $Oneline;
        $newTitle = preg_replace('%TITLE%',  $title, $newTitle,1 );
        ...?>
9
  • try this one stackoverflow.com/questions/13009227/… Commented Aug 30, 2016 at 13:12
  • @ KarthiVenture, I've tried that already, and maybe because instead of including the HEADER I'm generating it by PHP, it's not working that way. Commented Aug 30, 2016 at 13:20
  • can you echo your code while you get values/not ?? Commented Aug 30, 2016 at 13:24
  • Yes I did echo, the result is all lines of txt files. Commented Aug 30, 2016 at 13:30
  • i think problem is to retrive data from txt file. u just test with get value and apply to title with out file concept. Commented Aug 30, 2016 at 13:32

3 Answers 3

2

Please take a look at preg_replace(), the parameters are

preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

In your code you are using the variable $title to replace the pattern in $newTitle with a limit of one. I think you want to replace the text in the target file instead.

Update: There are two solutions that come into my mind right now:

  1. Instead of writing your text into the file directly, write it into a variable instead. This variable can be searched by preg_replace() and you can change your title dynamically. After you done that, write the variable into the targe file by e.g. fputs().
  2. Instead of replacing the title, set the title directly where it is needed, when you are writing the header section. Than there is no need for replacing.

I would recommend solution one. You know how to do that?

Sign up to request clarification or add additional context in comments.

4 Comments

Yes it's generating the target file and I'm trying to have title dynamically on target file.
So you say it yourself, you have to change the line IN the target file, or better before you write the data into the target file.
As I said the PHP file is creating, generating and writing contents into the HTML files (target files that primarily are not exist), so I cannot do anything with target files. And yes, it's better to write titles into target file dynamically, that is what I'm trying to do, but failing.
I did solution 2 already as I answered to my own question below, thanks.
0

As far as I can see, $newTitle is not defined prior to pre_replace.

Comments

0

After I couldn't solve the problem, I moved the retreiving data loop to above of the title and as Aaron suggested at this post It made it quiet simple as bellow:

<?php
for ($i = 1; $i <= 495; $i++)
{$j = 1;
$SousrceFile = @fopen($SousrceFile, 'r');
$TargetFile = fopen($TargetFile, 'w+');
while ($Oneline = @fgets($SousrceFile, 4096)) 
{$j = $j + 1;
if (strlen($Oneline) !==0)
$title = $Oneline;
fwrite($TargetFile, "<title>{$title}</title>\n");
...?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.