Log in

View Full Version : Resolved Custom Tags within the files HTML



midhul
09-08-2010, 03:25 PM
Hey,

Actually I am finding a way to parse the a custom html tag that I make within
the file using an included php.


<?php

include "taglib.php";

?>

<testtag attribute="">mytext</testtag>

If this is my PHP file,
what should I put in the "taglib.php"
so that is parses the <testtag> element and it's attributes?

I know using preg_match and other functions,
can be used to parse a string in common.

But how should I get the html within the file into the string to parse.

Hope you can help :)

Thanks

djr33
09-08-2010, 04:36 PM
You're approaching this backwards: PHP generates HTML, so it must be a string in PHP first, then output as HTML later.

$mytag = '<testtag attribute="">mytext</testtag>';

Creating a parser like this isn't that easy.


Alternatively you could use a function like file() or file_get_contents() to read a ".htm" file and output it. This is the opposite of include: you'd use a page to get the text from a file as a variable and go from there.

midhul
09-09-2010, 02:02 AM
I'm kinda looking for one which parses script withing the php file.

Any idea how some of those API's like Facebook and vBulletin do it?
Like "<fb:login></fb:login>" etc......

These tags can be put within an included page

djr33
09-09-2010, 02:06 AM
Facebook and vBulletin use a database and bring in the code (text) as a variable. It processes the variable then prints it to the page.

fileserverdirect
09-11-2010, 03:28 AM
This may be what your looking for:


<?php
function customtag($value, $text)
{
$body="The value in custom tag is $value, the custom text is $text";
//you can do whatever in here, such as call a database or whatnot.
return $body;
}
function replaceTags($body)
{
$body = preg_replace('!\<customtag=\"(.*?)\"\>(.*?)\<\/customtag\>!Uei', "''.customtag('$1','$2').''", $body);
//repeat above line for all other tags
return $body;
}
ob_start("replaceTags")
?>
<customtag="customvalue">customtext</customtag>
<!--***ALL OF YOUR HTML CODE HERE*** -->
<?
ob_end_flush();
?>

*Tested, Works, just make sure the page is less that 4MB (the default buffer size, actually it may be 4KB, I'd have to look that up :)) or you'll have to use ini_set to increase the buffer

midhul
09-13-2010, 10:01 AM
Thanks a lot for all your help guys!

--->Resolved<------------