SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
-
Apr 23, 2006, 17:33 #1
- Join Date
- Aug 2003
- Location
- Southern California, United States
- Posts
- 1,616
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Apr 23, 2006, 20:29 #2
- 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(
1 => '<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);
?>
-
Apr 24, 2006, 14:16 #3
- 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