Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP Preg Match All

Posted on 2009-07-05
5
Medium Priority
?
534 Views
Last Modified: 2012-05-07
I am trying to match UL's in an HTML file via PHP. I want to match #firstUL and get all of the LI's out of only that ID. Same thing for #secondLI.

preg_match_all("/id=\"firstUL(.+)\<\/div>/", $file, $match1);
preg_match_all("/<li><a href=\"(.*)\">(.*)<\/a><\/li>/", $match1[0][0], $match2);

The outcome I am looking for is:

[0] -> [0] = '/index.php', [1] = 'Example'
[1] -> [0] = '/index.php', [1] = 'Example'
[2] -> [0] = '/index.php', [1] = 'Example'
<div id="firstUL">
	<div class="someclass">
		<ul>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
		</ul>
		<ul>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
		</ul>
	</div>
</div>
<div id="secondUL">
	<div class="someclass">
		<ul>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
		</ul>
		<ul>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
			<li><a href="/index.php">Example</a></li>
		</ul>
	</div>
</div>

Open in new window

0
Comment
Question by:jwzk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24780360
You need the 's' pattern modifer to match newlines:

preg_match_all("/id=\"firstUL(.+)\<\/div>/s", $file, $match1);
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 24780404
For the result array you wanted, try this:
preg_match_all("/id=\"firstUL(.+)\<\/div>/s", $file, $match1);
preg_match_all("/<li><a href=\"(.*)\">(.*)<\/a><\/li>/", $match1[0][0], $match2);
$result = array();
for($i=0;$i<count($match2[1]);$i++)
  $result[]=array($match2[1][$i],$match2[2][$i]);
echo '<pre>';
var_dump($result);
echo '</pre>';

Open in new window

0
 

Author Comment

by:jwzk
ID: 24780416
Perfect, the only problem is the first preg_match_all is still getting both UL's.

This works, but is it the best method? I would prefer to not have to set the second one incase it changes.

preg_match_all("/id=\"firstUL(.+)\id=\"secondUL/s", $fileData, $match1);
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24780626
Use the 'U' (ungreedy) pattern modifier:

preg_match_all("/id=\"firstUL(.+)\<\/div>/sU", $file, $match1);
0
 

Author Closing Comment

by:jwzk
ID: 31599923
Perfect + the U modifier, thanks!
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question