I am working on a project to create PDF's from HTML code. I am using DOMpdf to do the creation, and it works perfectly, but I do have a question about preparing the HTML code to be sent to DOMpdf. If I have a variable, and it contains a string that is HTML code. How can I remove certain parts of that string? I know it is confusing, but here is an example. Here is a sample of the HTML that I am working with: <h1>This is a test page</h1> <!--remove start--> While the comb is open, you place the comb through the holes and close the plastic comb.<br><br> <!--remove end--> <p>...more text down here.</p> Code (markup): I want to remove all of the text beginning with <!--remove start--> and ending with <!--remove end-->. The problem I am having is that the text inside these 2 html comments is dynamic. Is there anyway to remove that text using PHP?
ereg_replace is my personal favourite. $string = ereg_replace('<!--removestart-->.*<!--removeend-->', '', $string); Something along that lines, eregi_replace or the perl regex functions can also do this.
or, you can use a php simple function: strip_tags string strip_tags ( string str [, string allowable_tags] ) This function tries to return a string with all HTML and PHP tags stripped from a given str. It uses the same tag stripping state machine as the fgetss() function. You can use the optional second parameter to specify tags which should not be stripped. Note: allowable_tags was added in PHP 3.0.13 and PHP 4.0b3.
The preg_* functions are supposed to be alot more efficient than the ereg_* functions. $string = preg_replace('~<!--removestart-->.*<!--removeend-->~Usi', '', $string); Code (markup):