Solved

PHP Preg Match All

Posted on 2009-07-05
5
525 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
  • 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

867 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now