Hi, I currently have a setup where I use a main php file that serves as the layout template for my website's pages. It's standard html but uses php bits like this for example:

<?php $contentfile = "./content/" . $id . ".txt"; include($contentfile);?>

What this bit does is simply add the content taken from a text file in my "content" directory. The "$id" variable is determined by the url.

So far, no problem. My next step is to replace all URLs in each content file. I dont want any php in these files so I tried the following:

<?php $contentfile = "./content/" . $id . ".txt"; include($contentfile); $path = str_replace("artoffact.php","aof.php",$path); ?>

The goal here was to replace all href tags that called artoffact.php with aof.php.

But it doesnt work 🙁
Can anyone help?

Thanx!

    You can't do it that way. str_replace() wants to replace what it finds in the variable $path (in your example). So unless the entire text file is inside a $path = ""; command then you aren't going to change anything.

    I believe that you have a straight text(HTML) file that you want to manipulate from your main page script. To do that you can't use include since all it does is insert all of the text there at that point. This is probably what you want to do.

    <?php
    $contentfile = "./content/" . $id . ".txt";
    $fp = fopen($contentfile, "r");

    $text = "";
    while (!feof($fp)) {
    $text .= fgets($fp, 4096);
    }

    $text = str_replace("artoffact.php","aof.php",$path);

    echo $text;
    ?>

      you haven't set $path yet 🙂 $path has to hold the parsed info you have gotten from the txt file. once you set that it should work.

        Wow, it worked 🙂
        I dont fully understand all the syntax behind it though. I will have to look into it. You just made one small mistake:

        You wrote:
        $text = str_replace("artoffact.php","aof.php",$path);

        When it should be:
        $text = str_replace("artoffact.php","aof.php",$text);

        In case anyone is wondering, the purpose of this was to use two different template files but keeping the same content. I had to make sure any links contained within the content files would refelect the proper template path.

        Thanx!

          You sound surprised. As for the typo that's what you get when you cut and paste, oh well had I actually run it I would have found the error. ;->

          What that piece of code is doing is reading in your file (that you were including) into the variable $text. Then running the string replace function on that text before printing it out. Just including it will basically write all of the text from the file into the space in your script that is currently occupied by the include $file; line.

            Heh, I wasn't surprised, but rather very gratefull. Everything I had tried so far didnt work and while the learning curve isnt that steep, there's much I still need to understand to achieve everything I have planned. Thanks for explaining that bit, I'll experiment some more with it...

            -steph

              Write a Reply...