how to replace certain tag from first and last ??

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

how to replace certain tag from first and last ??

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

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

$newString = preg_replace($patterns, '', $oldString);
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

Code: Select all

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