note 110280 added to function.strip-tags
| From: | bzplan at web dot de | Date: | Sun, 07 Oct 2012 07:57:42 +0000 |
| Subject: | note 110280 added to function.strip-tags | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-191170@lists.php.net to get a copy of this message | ||
a HTML code like this:
<?php
$html = '
<div>
<p style="color:blue;">color is blue</p><p>size is <span
style="font-size:200%;">huge</span></p>
<p>material is wood</p>
</div>
';
?>
with <?php $str = strip_tags($html); ?>
... the result is:
$str = 'color is bluesize is huge
material is wood';
notice: the words 'blue' and 'size' grow together :(
and line-breaks are still in new string $str
if you need a space between the words (and without line-break)
use my function: <?php $str = rip_tags($html); ?>
... the result is:
$str = 'color is blue size is huge material is wood';
the function:
<?php
// --------------------------------------------------------------
function rip_tags($string) {
// ----- remove HTML TAGs -----
$string = preg_replace ('/<[^>]*>/', ' ', $string);
// ----- remove control characters -----
$string = str_replace("\r", '', $string); // --- replace with empty space
$string = str_replace("\n", ' ', $string); // --- replace with space
$string = str_replace("\t", ' ', $string); // --- replace with space
// ----- remove multiple spaces -----
$string = trim(preg_replace('/ {2,}/', ' ', $string));
return $string;
}
// --------------------------------------------------------------
?>
the KEY is the regex pattern: '/<[^>]*>/'
instead of strip_tags()
... then remove control characters and multiple spaces
:)
----
Server IP: 69.147.83.199
Probable Submitter: 188.193.218.26
----
Manual Page -- http://www.php.net/manual/en/function.strip-tags.php
Edit -- https://master.php.net/note/edit/110280
Del: integrated -- https://master.php.net/note/delete/110280/integrated
Del: useless -- https://master.php.net/note/delete/110280/useless
Del: bad code -- https://master.php.net/note/delete/110280/bad+code
Del: spam -- https://master.php.net/note/delete/110280/spam
Del: non-english -- https://master.php.net/note/delete/110280/non-english
Del: in docs -- https://master.php.net/note/delete/110280/in+docs
Del: other reasons-- https://master.php.net/note/delete/110280
Reject -- https://master.php.net/note/reject/110280
Search -- https://master.php.net/manage/user-notes.php