Click to See Complete Forum and Search --> : how to replace certain tag from first and last ??
PHPycho
11-29-2007, 02:39 AM
Hello forums..
Can anybody give a hint for how to replace the first <ul> and last </ul> tag from the string $string:
$string = '<ul>
<li></li>
<li>
<ul>
<li>
</li>
<li></li>
</ul>
</li>
<li></li>
</ul>';
Thanks in advance to all of you.
trymbill
11-29-2007, 05:21 AM
To replace <ul> with <ba> use this:
$string = '<ul>
<li></li>
<li>
<ul>
<li>
</li>
<li></li>
</ul>
</li>
<li></li>
</ul>';
$string = ereg_replace('<ul>','<ba>',$string);
$string = ereg_replace('</ul>','</ba>',$string);
echo $string; // Returns: <ba> ... </ba>
trymbill
11-29-2007, 05:23 AM
Ah, I'm sorry, I misunderstood your question! :)
To replace the first <ul> and last </ul> ... I'm not sure :)
sstalder
11-29-2007, 10:00 AM
Not sure how stable this is but it will do the trick in most cases:
$string = '<ul>
<li></li>
<li>
<ul>
<li>
</li>
<li></li>
</ul>
</li>
<li></li>
</ul>';
$newString = substr($string, 4, strlen($string)-9);