0

I'm trying to make a php site read from a text file by using this:

<html>

    <head>
        <title>This is a test!</title>
    </head>
    <body>

        <?php

            $f = fopen("testfile.txt", "r");

            // Read line by line until end of file
            while(!feof($f)) { 
                echo fgets($f) . "<br />";
            }

            fclose($f);

        ?>

    </body>
</html>

-and it works!

Next step is to make the php site read text with links inside. This is the text in the text file within a link, please press this link www.stackoverflow.com and more text to come after the link. Linktext like "THIS IS A LINK" is also needed. Hope you understand what I want:)

How do I do something like this?

sectus
  • 15,605
  • 5
  • 55
  • 97
  • 1
    possible duplicate of [Extract URLs from text in PHP](http://stackoverflow.com/questions/910912/extract-urls-from-text-in-php) – Quentin Sep 10 '14 at 09:00
  • Note---> I'm trying to make a php site with the possibility of changing some of the text lines on the webpage from a text file, so that I only need to change one text file if I want some text edited. I want seperate text files for the different text sections on the site. – Birk Monrad Christensen Sep 10 '14 at 09:02
  • On a somewhat related note, you may be interested to read about [Jekyll](http://jekyllrb.com/), which generates static sites from plain text/markdown content. – Alex Sep 10 '14 at 10:23

2 Answers2

0

From reading your: "I want seperate text files for the different text sections on the site."

Have you considered using a separate php file "setting.php" and inside making a multidimensional array with all the things you need?

<?php
$settings = array(
    'header_text' => array(
        'text' => 'This is my header text and it containts a link <a href="website-url.com">click here</a>',
        'color' => '#000000',
        'font-size' => '12px'
    ),
    'footer_text' => array(
        'text' => 'Copyright &copy; <a href="/tncs.php">Terms and Conditions</a>',
        'color' => '#000000',
        'font-size' => '12px'
    ),
);
?>

and if you need to go a step deeper and do it PER page:

<?php
$settings = array(
    'index.php' => array(
        'header_text' => array(
            'text' => 'This is my header text and it containts a link <a href="website-url.com">click here</a>',
            'color' => '#000000',
            'font-size' => '12px'
        ),
        'footer_text' => array(
            'text' => 'Copyright &copy; <a href="/tncs.php">Terms and Conditions</a>',
            'color' => '#000000',
            'font-size' => '12px'
        )
    ),
    'about.php' => array(
        'header_text' => array(
            'text' => 'This is my header text and it\'s only for the about page and it containts a link <a href="website-url.com">click here</a>',
            'color' => '#000000',
            'font-size' => '12px'
        ),
        'footer_text' => array(
            'text' => 'Copyright &copy; <a href="/tncs.php">Terms and Conditions</a> and go to the main index page',
            'color' => '#000000',
            'font-size' => '12px'
        )
    ),
);
?>

you can then include this file in your main script

include('settings.php');

and then call upon the array and such, all you do it edit the php file, and any changes would update.

This way you can manually edit any details you want!

Example:

<?php include('settings.php'); $curr_page = basename($_SERVER['PHP_SELF']); ?>

<div style="color: <?php echo $settings['header_text']['color']?>"><?php echo $settings[$curr_page]['header_text']['text']?></div>

<div style="color: <?php echo $settings[$curr_page]['header_text']['color']?>"><?php echo $settings[$curr_page]['header_text']['text']?></div>
James Lalor
  • 1,236
  • 9
  • 22
0

Thanks for your great answer - it was easy to understand. I need to make my php site read from a text file because it is my boss who needs to change text on the site, and I don't want him to delete any of the coding. So, is there any way to make the php site read from a text file?

This is a text 1 (must be read from document 1)

This is another text (must be read from document 2)

This is another text section on the php and THESE WORDS shuld be a hyperlink in the middle of the sentence.

etc...

Thanks again!