0

i have a problem and need your help. i want use my custom tag in my script code like [tag] and analyze all html code then parser codes and Replace these tags with php code or my output world and echo my output

a simple code :

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>[MY_PAGE_TITLE]</title>
</head>
    <header></header>
        <div class="left">[MY_WEBSITE_LEFT_SIDEBAR_INFORMATION]</div>
            <article>[MY_WEBSITE_ARTICLE]</article>
        <div class="right">[MY_WEBSITE_RIGHT_SIDEBAR_INFORMATION]</div>
    <footer></footer>
<body>
</body>
</html>

my first tag [MY_PAGE_TITLE] should be replaced with one world like "my website" i want use this for my language website. i get this word from a array like :

mylangarray [
     MY_PAGE_TITLE="my website"
]

but for my any other tag i want load some module to left or right sidebar or load some article from my DB

How can I do this?

Morteza
  • 41
  • 4
  • This isn't really a regex problem. Why not just use standard string substitution as supported by php? Also, be aware that putting `regex` and `html` together (especially when it comes to parsing) invites a _lot_ of downvoting. Since your question doesn't require regex as far as I can understand, I'd suggest removing the regex tag lest you get downvoted to oblivion by users too quick to judge. – mechalynx Sep 02 '14 at 20:03
  • @Morteza You should take a look at the source code of [**Smarty**](http://www.smarty.net/) which is a template system made in PHP, that will give you a lot of ideas on how to do what you described above and more. – Prix Sep 02 '14 at 20:25
  • Thanks guys. ivy_lynx, i deleted regex tag from my question. and Prix i looked it but it's not like my asking. – Morteza Sep 20 '14 at 21:01
  • anybody is here to help me? help please!! – Morteza Oct 01 '14 at 14:02

1 Answers1

1

i solved my problem. if you have this problem, you can use this method.

step1: you define your custom tag in your main view file and then in the server side with PHP ob_start function run output buffer then require or require_once or include or include_once your file. now with define a variable and ob_get_contents function in PHP, store all file code in a variable. like this:

$html = ob_get_contents();

now you have all view file in a variable.

you can clean your buffer with ob_end_clean function.

example:

ob_start();
require_once 'mainViewFile.php';
$html = ob_get_contents();
ob_end_clean();

step2: you can use str_replace funcation for replace your defined custom tag with your new data.

$leftSideBarInformation = 'We stored left bar information in this variable!';
$html = str_replace('[MY_WEBSITE_LEFT_SIDEBAR_INFORMATION]', $leftSideBarInformation, $html);

if you have array of custom tag you can use a foreach loop.

Morteza
  • 41
  • 4