note 82733 added to function.substr-replace
| From: | WilliamBarry at osu1 dot php dot net | Date: | Wed, 23 Apr 2008 19:18:44 +0000 |
| Subject: | note 82733 added to function.substr-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138710@lists.php.net to get a copy of this message | ||
I recently ran across a situation where I need to strip a heavily nested html list such that only
the top level was preserved. I started with a regular expression solution, but found that I kept
matching the wrong closing ul with an outer opening ul.
This was my alternative solution, and it seems to work well:
<?php
function stripNestedLists($str)
{
$str2 = $str;
$lastStr = $str2;
do
{
// Find the first closing ul
$cul = strpos($str2, '</ul>');
$ul = 0;
$lastUL = 0;
do
{
// Find the next opening ul
$lastUL = $ul;
$ul = strpos($str2, '<ul', $ul+1);
}
while ($ul !== false && $ul < $cul);
$lastStr = $str2;
$str2 = substr_replace($str2, '', $lastUL, $cul-$lastUL+5);
$str2 = trim($str2);
}
while (strlen($str2) > 0);
return $lastStr;
}
?>
Hope this helps someone.
----
Server IP: 208.69.120.35
Probable Submitter: 130.160.166.106
----
Manual Page -- http://www.php.net/manual/en/function.substr-replace.php
Edit -- https://master.php.net/note/edit/82733
Del: integrated -- https://master.php.net/note/delete/82733/integrated
Del: useless -- https://master.php.net/note/delete/82733/useless
Del: bad code -- https://master.php.net/note/delete/82733/bad+code
Del: spam -- https://master.php.net/note/delete/82733/spam
Del: non-english -- https://master.php.net/note/delete/82733/non-english
Del: in docs -- https://master.php.net/note/delete/82733/in+docs
Del: other reasons-- https://master.php.net/note/delete/82733
Reject -- https://master.php.net/note/reject/82733
Search -- https://master.php.net/manage/user-notes.php