note 101236 added to function.preg-replace
| From: | (dot) | Date: | Sat, 04 Dec 2010 05:58:32 +0000 |
| Subject: | note 101236 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174606@lists.php.net to get a copy of this message | ||
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));
?>
----
Server IP: 69.147.83.197
Probable Submitter: 24.203.3.229
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- https://master.php.net/note/edit/101236
Del: integrated -- https://master.php.net/note/delete/101236/integrated
Del: useless -- https://master.php.net/note/delete/101236/useless
Del: bad code -- https://master.php.net/note/delete/101236/bad+code
Del: spam -- https://master.php.net/note/delete/101236/spam
Del: non-english -- https://master.php.net/note/delete/101236/non-english
Del: in docs -- https://master.php.net/note/delete/101236/in+docs
Del: other reasons-- https://master.php.net/note/delete/101236
Reject -- https://master.php.net/note/reject/101236
Search -- https://master.php.net/manage/user-notes.php