parsing - How to parse data from txt file using PHP with the following format -
how parse data txt file using php? here variables.txt file
title=job=test images=image1.jpg,image2.jpg,image3.jpg images2=image3.jpg,image2.jpg,image1.jpg
and here html file template
<!doctype html> <html> <head> <title>++title++</title> </head> <body> <h1>++title++</h1> <p>this template file have swap variables between double plus signs (++var++) values in variables.txt.</p> <p>you should write class template engine. engine should support following functions: replace variables, include subtemplates, support loops.</p> <ul> --for ++images++ image-- <li>--include image.html image--</li> --endfor-- </ul> <ul> --for ++images2++ image-- <li>--include image.html image--</li> --endfor-- </ul> </body> </html>
i have images folder 3 images image1.jpg, image2.jpg, , image3.jpg
the output should like
job=test
- image1.jpg here
- image2.jpg here
image3.jpg here
image3.jpg here
- image2.jpg here
- image1.jpg here
i tried this;
function parsetext($text) { $exp = explode("\n", $text); $newdatas = array(); foreach ($exp $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 template file have swap variables between double plus signs (++var++) values in variables.txt.</p> <p>you should write class template engine. engine should support following functions: replace variables, include subtemplates, support loops.</p> <ul> <?php foreach(gettextkey("images", ",") $image) { ?> <li><?php include($image.".html"); ?></li> <?php } ?> </ul> <ul> <?php foreach(gettextkey("images2", ",") $image) { ?> <li><?php include($image.".html"); ?></li> <?php } ?> </ul> </body> </html>
Comments
Post a Comment