SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Complicated Preg_Match_All Problem

    I have the following:
    preg_match_all('/<\b([^<>]*)>/', $structure[1], $objects);

    The structure is my document that is being searched, and the objects is the variable. The problem is, I don't want the code to find another result until the first tag name has been closed.

    For example:
    <game><i>Pacman</i></game>

    The above code will find 'game' and 'i' as a result, even though the tag name <game> has not been closed yet (</game>). So in reality it should only find 'game' as a result, ignoring 'i' because 'game' has not been closed yet. Can someone show me the correct preg_match_all I need. Thank you so much.
    Have a good day.

  2. #2
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?PHP
    error_reporting
    (E_ALL|E_STRICT);

    $structure = array(
        
    => '<game><i>Pacman</i></game>
        <game><i>Centipede</i></game>
        <game><b>Defender</b></game>'
    );
        
    $regex '#<(\w+)(?:\s[^>]*)?>.*?</\1>#';

    $result preg_match_all($regex$structure[1], $objects);

    print 
    "Result = $result\n";
    var_dump($objects);
    ?>
    Austin Hastings - Principal Consultant - Longacre, Inc.

    Anything you can do, you can do better.

  3. #3
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks it worked!
    Have a good day.


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
  •