Page 1 of 1

how to replace certain tag from first and last ??

Posted: Thu Nov 29, 2007 2:36 am
by PHPycho
Hello forums..
Can anybody give a hint for how to replace the first <ul> and last </ul> tag from the string $string:

Code: Select all

$string = '<ul>
  <li></li>
  <li>
    <ul>
      <li>        
      </li>
      <li></li>     
    </ul>
  </li>
  <li></li>
</ul>';
Thanks in advance to all of you.

Posted: Thu Nov 29, 2007 4:22 am
by Jenk

Code: Select all

$patterns = array (
  '/^' . preg_quote('<ul>', '/') . '/i',
  '/' . preg_quote('<ul>', '/') . '$/i',
);

$newString = preg_replace($patterns, '', $oldString);

Posted: Thu Nov 29, 2007 10:39 am
by GeertDD

Code: Select all

preg_replace('#^<ul>|</ul>$#i', '', $string);