SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to replace certain tag from first and last ??

    Hello forums..
    Can anybody give a hint for how to replace the first <ul> and last </ul> tag from the string $string:
    PHP Code:
    $string '<ul>
      <li></li>
      <li>
        <ul>
          <li>        
          </li>
          <li></li>     
        </ul>
      </li>
      <li></li>
    </ul>'

    Thanks in advance to all of you.

  2. #2
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the string looks like that, just cut off the first and last 4 chars

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $result preg_replace('~^<ul>|</ul>$~i'''$subject); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Member
    Join Date
    May 2006
    Location
    Transylvania
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to replace in this specific string:
    PHP Code:
    echo $newtagopen substr($string4, -5)  . $newtagclose
    or just use regex for a general case
    Last edited by silversk8r; Nov 29, 2007 at 02:57. Reason: php tags added

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Might want to give PHP's DOM extension a try:
    http://uk2.php.net/dom


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •