note 101236 deleted from function.preg-replace by crell

From: Date: Fri, 01 Jul 2022 19:49:15 +0000
Subject: note 101236 deleted from function.preg-replace by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218863@lists.php.net to get a copy of this message
Note Submitter: Terminux (dot) anonymous at gmail ---- This function will strip all the HTML-like content in a string. I know you can find a lot of similar content on the web, but this one is simple, fast and robust. Don't simply use the built-in functions like strip_tags(), they dont work so good. Careful however, this is not a correct validation of a string ; you should use additional functions like mysql_real_escape_string and filter_var, as well as custom tests before putting a submission into your database. <?php $html = <<<END <div id="function.preg-split" class="refentry"> Bonjour1 \t <div class="refnamediv"> Bonjour2 \t <h1 class="refname">Bonjour3 \t</h1> <h1 class=""">Bonjour4 \t</h1> <h1 class="*%1">Bonjour5 \t</h1> <body>Bonjour6 \t<//body>> </ body>Bonjour7 \t<//// body>> < a href="image.php" alt="trans" / > some leftover text... < DIV class=noCompliant style = "text-align:left;" > ... and some other ... < dIv > < empty> </ empty> <p> This is yet another text <br > that wasn't <b>compliant</b> too... <br /> </p> <div class="noClass" > this one is better but we don't care anyway </div ><P> <input type= "text" name ='my "name' value = "nothin really." readonly> end of paragraph </p> </Div> </div> some trailing text END; // This echoes correctly all the text that is not inside HTML tags $html_reg = '/<+\s*\/*\s*([A-Z][A-Z0-9]*)\b[^>]*\/*\s*>+/i'; echo htmlentities( preg_replace( $html_reg, '', $html ) ); // This extracts only a small portion of the text echo htmlentities(strip_tags($html)); ?>

« previous php.notes (#218863) next »