Remove HTML from current page being parsed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Remove HTML from current page being parsed

Post by thomas777neo »

Hi All

I load all my php pages through one central page i.e. container.php

It looks something like this:

Code: Select all

<?php
require("header.php");

require("page.php");

require("footer.php");
?>
The problem is this: I am trying to generate a PDF document. But because there is HTML in the header.php the PDF generation page show a whole bunch of garbage. Is there anyway to parse the page.php and remove the HTML at the same time. I understand that I can put if statements for the header and footer. Can it be done? Removing HTML on php execution, could be quite handy.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

putting if's is the basic way.. seperating html from code is the better way.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

you could also use output buffering, but its kinda like a band aid for this.

[php_man]ob_start()[/php_man]

[php_man]ob_end_clean()[/php_man]
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Thanks

Post by thomas777neo »

Feyd, could you please show me a simple example. rehfeld, I tried the ob_ functions, it cleans up all the garbage but it doesn't remove the HTML.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

here is a fairly simple example of template usage.
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

I use this kind of thing for making a printable version of my page by removing most colors and making the page more document like by disabling all the links and forms etc.

I see no reason why it couldnt be used to remove html tags, but for that you'll need preg_raplace rather than str_replace.

Strip_Tags may also be of use.

Code: Select all

function links_off($a)
{
  // remove links
  $a  = preg_replace("/<a(.*?)>/i", "<U>", $a);
  $a  = preg_replace("/<\/a>/i", "</U>", $a);
  $a  = preg_replace("/<A(.*?)>/i", "<U>", $a);
  $a  = preg_replace("/<\/A>/i", "</U>", $a);

  // replace table css classes 
  $a  = str_replace("Taulu0", "TauluprintB", $a);

  // Then i remove lot's of other stuff with other commands like above 

  return $a;
}

ob_start("links_off");

// Here i generate my page

ob_end_flush();

feyd | use the

Code: Select all

tags.. [/color]
Post Reply