Solved

How do I extract values with pregmatch if located within <UL> tags

Posted on 2015-02-24
3
57 Views
Last Modified: 2015-02-24
How do I correctly structure this pregmatch to get each subcategory name which is only located between <ul> tags.
(I do not want values that are not located within <ul> tags.  Is this possible?)


<ul>  
<li>
<a href="/ammo-case" title="">Case</a>
</li><li>
<a href="/ammo-pistol" title="">Pistol</a>
</li><li>
<a href="/ammo-rifle" title="">Rifle</a>
</li><li>
<a href="/ammo-shotgun" title="">Shotgun</a>
</li></ul>

Open in new window



$statement = $conn->prepare('INSERT INTO SubCats (SubCatName) VALUES(:SubCatName)');

preg_match_all('%<li>\s*<a href=".*?" title="">(.*?)<\/a>\s*<\/li>%si',$buffer1,$matches1);
	
foreach($matches1[0] as $key=>$link){
	$params = array(':SubCatName'=>$matches1[1][$key]);
	$statement->execute($params);	
}		

Open in new window

0
Comment
Question by:lawrence_dev
3 Comments
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 150 total points
ID: 40628552
Check out  my article on using Simple HTML DOM to parse HTML and get to values.

http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_10277-HTML-Manipulation-Made-Easy.html

Using regex with HTML can be a fragile thing. A simple HTML change (any change in structure, spacing, attributes, etc...) can break a working setup. Using a DOM parser will turn the page into something that is easy to search and should work fairly consistently, even if the page goes through some minor changes.
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 350 total points
ID: 40629769
@gr8gonzo gives good advice. Here's a pattern anyway though if you choose to use it:
preg_match_all('%<li>\s*<a href=".*?" title="">(.*?)<\/a>\s*<\/li>(?=((?!<ul[>\s]).)*</ul>)%si',$buffer1,$matches1);

Open in new window

0
 

Author Closing Comment

by:lawrence_dev
ID: 40629774
Thanks Terry and Gr8Gonzo!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 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