Hello forums..
Can anybody give a hint for how to replace the first <ul> and last </ul> tag from the string $string:
Thanks in advance to all of you.PHP Code:$string = '<ul>
<li></li>
<li>
<ul>
<li>
</li>
<li></li>
</ul>
</li>
<li></li>
</ul>';
| SitePoint Sponsor |
Hello forums..
Can anybody give a hint for how to replace the first <ul> and last </ul> tag from the string $string:
Thanks in advance to all of you.PHP Code:$string = '<ul>
<li></li>
<li>
<ul>
<li>
</li>
<li></li>
</ul>
</li>
<li></li>
</ul>';




If the string looks like that, just cut off the first and last 4 chars
Turnware MVC - A new barebone, fast and flexible PHP5 framework!
Ruben Knol
Turnware MVC Lead Developer
PHP Code:$result = preg_replace('~^<ul>|</ul>$~i', '', $subject);
to replace in this specific string:
or just use regex for a general casePHP Code:echo $newtagopen . substr($string, 4, -5) . $newtagclose;
Last edited by silversk8r; Nov 29, 2007 at 03:57. Reason: php tags added
That regex will only work, if there is nothing before or after the ul and if there is only one ul at the top level. If you want it to work generic, you need to write a parser.





Might want to give PHP's DOM extension a try:
http://uk2.php.net/dom
Bookmarks