tags:

views:

172

answers:

7

I have a variable $example that has html content that I need to render/echo to screen. But before rendering to screen, I want to remove certain elements. For instance the structure of the $example variable is:

<form> some stuff here... </form><p>hi there</p><div></div>

I want to remove the form element from the $example variable before rendering. I know I can do this using regular expressions, but what is a better way to do it in php? Also, is there a function in php like strip_tags, which can be passed exactly the tag that it needs to strip? My question is not related to stripping unsafe tags or cleaning up html, it is just that i want to remove certains elements from a variable before outputting. As a generatlization, how do i remove only those divs from the $example variable that have a particular class added to them?

+4  A: 

Don't use PHP to munge the HTML, but instead use a print stylesheet to change the layout.

http://www.webcredible.co.uk/user-friendly-resources/css/print-stylesheet.shtml

Edit after your clarification:
I still think stylesheets are the way to go to hide content. If you're not stripping tags for security purposes, then I don't really know what you're trying to achieve exactly by stripping content server-side.

Joeri Sebrechts
I dont think my question is related to the print style sheet at all. I am asking how to remove certain elements from a string that will be printed as html on the screen.
+1  A: 

Use the display css attribute in your print css:

Your html balise to your print.css file:

<link rel="stylesheet" type="text/css" media="print" href="print.css" />

Your form description:

<form class="pouet">
    ...
</form>

Your print.css file

.pouet {
    display:none;
}
tinmaru
i dont just wnat to set its display property after printing it. the reason is that i might have printed the form separately before pringint the $example variable. Any styles i apply to the one in $example's form will be applied to the one outside, whcih is not what i want
+2  A: 

You can pass strip_tags a list of allowed tag names with the second parameter $allowable_tags:

// strip any tag except P and DIV
$clean = strip_tags($str, '<p><div>');

And if you want to do a more adjustable filtering, I’d use a HTML parser like DOMDocument, traverse the DOM tree and remove the nodes I don’t want.

Gumbo
I think the DOMDocument approach seems promising, except that when i try to load the string into it, it starts issuing warnings becuase the string contains stuff like ' ' etc. cannot figure out how to do it this way.
@m.u.sheikh: Did you use `loadHTML` to load the HTML document?
Gumbo
No, I was using loadXML, now I have corrected it by using loadHTML, it is loading fine, and i am now trying to traverse it appropriately to reach those elements I dont want, to delete them.
+1  A: 

If you are using XHTML, you can use XML tools (like DOM extension or SimpleXML) for your usage.

mere-teresa
+1  A: 
preg_replace($regex, "", $example);

I am not very good at regex, but $regex would contain form open tag followed by a wildcard and then form close tag, thus removing everything contained inside

Edit: $regex = "/<form.*\/form>/s";

woody993
+1  A: 
OfficeJet
A: 

You could use explode() to split the sting by the <form> and </form> tags, and just include the chunks you want:

$arr1 = explode("<form>",$example);
$example = $arr1[0];
for ($i=1;$i<count($arr1);$i++)
{
    $arr2 = explode("</form>",$arr1[$i]);
    $example .= $arr2[1];
}
Waggers