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 );
        ...?>
Saasaan
  • 83
  • 1
  • 7
  • try this one http://stackoverflow.com/questions/13009227/php-how-to-change-title-of-the-page-after-including-header-php – Karthi Aug 30 '16 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. – Saasaan Aug 30 '16 at 13:20
  • can you `echo` your code while you get values/not ?? – Karthi Aug 30 '16 at 13:24
  • Yes I did echo, the result is all lines of txt files. – Saasaan Aug 30 '16 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. – Karthi Aug 30 '16 at 13:32
  • It retrieving data from txt files, I think I can figure out to retrieve the only first line, but most important part of the problem is that I could not change %TITLE% even with a static pre-defined variable. – Saasaan Aug 30 '16 at 13:37
  • can you post balance code? – Karthi Aug 30 '16 at 13:39
  • Why are you replacing the title anyway. Why not writing the tags and the title directy to the target file? – Philipp Palmtag Aug 30 '16 at 13:55
  • You can write a template, then use template engine such as [Smarty](http://www.smarty.net/) to explain it, and use ob to output html pages. – SuperBear Aug 31 '16 at 06:21

3 Answers3

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?

Philipp Palmtag
  • 1,310
  • 2
  • 16
  • 18
  • Yes it's generating the target file and I'm trying to have title dynamically on target file. – Saasaan Aug 30 '16 at 14:02
  • 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. – Philipp Palmtag Aug 30 '16 at 14:22
  • 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. – Saasaan Aug 30 '16 at 15:50
  • I did solution 2 already as I answered to my own question below, thanks. – Saasaan Aug 31 '16 at 06:28
0

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

Caspar Wylie
  • 2,818
  • 3
  • 18
  • 32
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");
...?>
Community
  • 1
  • 1
Saasaan
  • 83
  • 1
  • 7