0

I'm currently working on separating HTML & PHP code here's my code which is currently working for me.

code.php

<?php
$data['#text#'] = 'A';

$html = file_get_contents('test.html');

echo $html = str_replace(array_keys($data),array_values($data),$html);
?>

test.html

<html>
<head>
<title>TEST HTML</title>
</head>
<body>
<h1>#text#</h1>
</body>
</html>

OUTPUT: A

it search and change the #text# value to array_value A it works for me.

Now i'm working on a code to search "id" tags on html file. If it's searches the "id" in ".html" file it will put the array_values in the middle of >

EX: <div id="test"> **aray_values here** </div>

test.php

<?php

$data['id="test"'] = 'A';

$html = file_get_contents('test.html');

foreach ($data as $search => $value)
{
    if (strpos($html , $search))
    {
        echo 'FOUND';
        echo $value;
    }
}

?>

test.html

<html>
<head>
<title>TEST</title>
</head>
<body>
<div id="test" ></div>
</body>
</html>

My problem is I don't know how to put the array_values in the middle of every ></ search in the .html file.

Desired OUTPUT: <div id="test" >A</div>

Detention
  • 167
  • 1
  • 1
  • 10
  • Might be easier to use a PHP template engine (http://www.smarty.net/) or with your setup as is, PHP DOMDocument (http://php.net/manual/en/class.domdocument.php). – Will Nov 07 '13 at 18:46
  • i don't want to used any third party – Detention Nov 07 '13 at 18:47
  • If it always looks like that, you can replace `id="test" ><` with `id="test" >VALUE<`. But i wouldn't tie myself to the HTML structure at all. In practice, you will probably need to output more than just values inside elements. Sometimes you might need to add extra elements or attributes of elements depending on the context. – GolezTrol Nov 07 '13 at 18:48
  • Ok, then DOMDocument or (more haphazardly) str_replace() or preg_replace() are the options. DOMDocument->loadHTML(""), getElementById, update the value, DOMDocument->saveHTML. Not third-party, valid since PHP 4.1 – Will Nov 07 '13 at 18:50
  • good Ideas but I want to figure out what I just started ^_^ – Detention Nov 07 '13 at 18:53

2 Answers2

2

function callbackInsert($matches)
{
    global $data;
    return $matches[1].$matches[3].$matches[4].$data[$matches[3]].$matches[6];
}


$data['test'] = 'A';

$html = file_get_contents('test.html');

foreach ($data as $search => $value)
{
    preg_replace_callback('#(<([a-zA-Z]+)[^>]*id=")(.*?)("[^>]*>)([^<]*?)(</\\2>)#ism', 'callbackInsert', $html);
}

Warning: code is not tested and could be improved - re global keyword and what items are allowed between > and

Regular expression explanation:

(<([a-zA-Z]+) - any html tag starting including the last letter of the tag
[^>]* - anything that is inside a tag <>
id=")(.*?)(" - the id attribute and its value
[^>]* - anything that is inside a tag <>
>) - the closing tag
([^<]*?) - anything that is not a tag, tested by opening a tag <
(</\\2>) - the closing tag matching the 2nd bracket, ie. the matching opening tag
jdog
  • 2,465
  • 6
  • 40
  • 74
  • is it possible not using a function? – Detention Nov 07 '13 at 19:55
  • Yes, but in my experience the function makes it much easier to debug. Have a look at php.net/preg_replace_callback, you can have the function in a class or object as well – jdog Nov 07 '13 at 20:01
0

Use views (.phtml) files to dynamically generate content. This is native for PHP (no 3rd party required).

See this answer: What is phtml, and when should I use a .phtml extension rather than .php?

and this: https://stackoverflow.com/questions/62617/whats-the-best-way-to-separate-php-code-and-html

Community
  • 1
  • 1
Maciej Sz
  • 11,151
  • 7
  • 40
  • 56