-6

How do I parse a data from txt file using php? here is my variables.txt file

TITLE=Job=test
IMAGES=image1.jpg,image2.jpg,image3.jpg
IMAGES2=image3.jpg,image2.jpg,image1.jpg

and here is the html file template

<!DOCTYPE html>
<html>
    <head>
        <title>++TITLE++</title>
    </head>
    <body>
        <h1>++TITLE++</h1>
        <p>This is a template file where you have to swap the variables between the double plus signs (++VAR++) with the values in variables.txt.</p>
        <p>You should write a class that is the template engine. This engine should support the following functions: replace variables, include subtemplates, support loops.</p>
        <ul>
        --for ++IMAGES++ as IMAGE--
            <li>--include image.html with IMAGE--</li>
        --endfor--
        </ul>
        <ul>
        --for ++IMAGES2++ as IMAGE--
            <li>--include image.html with IMAGE--</li>
        --endfor--
        </ul>
    </body>
</html>

I have an images folder with 3 images image1.jpg, image2.jpg, and image3.jpg

The output should look like

Job=test

  • image1.jpg here
  • image2.jpg here
  • image3.jpg here

  • image3.jpg here

  • image2.jpg here
  • image1.jpg here
Rhob Gatchalian
  • 121
  • 1
  • 3
  • 7
  • I'd use `preg_replace_callback`. Is title = `Job=test`? What is `++VAR++`? – chris85 Jan 27 '16 at 13:28
  • 1
    Looks like homework, or a competency test. Neither should be offloaded to SO. **if** you have tried something thats not working, add your code and explain what issues you are having. If not this will be closed – Steve Jan 27 '16 at 13:31
  • Hint: google "PHP INI FILE", "PHP EXPLODE", and "PHP REPLACE". – hherger Jan 27 '16 at 13:39
  • Possible duplicate of [PHP Parse HTML code](http://stackoverflow.com/questions/3627489/php-parse-html-code) – Shaohao Jan 27 '16 at 13:57
  • @chris85 ++VAR++ are the variables in the html template that should be replace to its values in the txt file – Rhob Gatchalian Jan 28 '16 at 00:22

1 Answers1

1

I tried something like this;

function parseText($text) {

    $exp = explode("\n", $text);
    $newDatas = array();

    foreach ($exp as $row) {
        if (strpos($row, "=") !== false) {
            $keyVals = explode("=", $row);
            $newDatas[$keyVals[0]] = $keyVals[1];
        }
    }

    return $newDatas;
}


function getTextKey($key, $explodeWith = "") {
    $text = "TITLE=Job=test\nIMAGES=image1.jpg,image2.jpg,image3.jpg\nIMAGES2=image3.jpg,image2.jpg,image1.jpg";
    if ($explodeWith == "") {
        $data = parseText($text)[$key];
    } else {
        $data = explode($explodeWith,  parseText($text)[$key]);

    }
    return $data;
}

?>

<!DOCTYPE html>
<html>
    <head>
        <title><?php echo getTextKey("TITLE"); ?></title>
    </head>
    <body>
        <h1><?php echo getTextKey("TITLE"); ?></h1>
        <p>This is a template file where you have to swap the variables between the double plus signs (++VAR++) with the values in variables.txt.</p>
        <p>You should write a class that is the template engine. This engine should support the following functions: replace variables, include subtemplates, support loops.</p>
        <ul>
        <?php
        foreach(getTextKey("IMAGES", ",") as $image) 
        { 
        ?>
            <li><?php include($image.".html"); ?></li>
        <?php 
        }
        ?>
        </ul>
        <ul>
        <?php
        foreach(getTextKey("IMAGES2", ",") as $image) 
        { 
        ?>
            <li><?php include($image.".html"); ?></li>
        <?php 
        }
        ?>
        </ul>
    </body>
</html>
Mert Öksüz
  • 1,071
  • 6
  • 17
  • i meant the txt to be a separate file named as "variables.txt" file which has the data above, could you help me maybe like using file_get_contents() function? – Rhob Gatchalian Jan 28 '16 at 00:40