Solved

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

Posted on 2015-02-24
3
52 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
Comment Utility
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
Comment Utility
@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
Comment Utility
Thanks Terry and Gr8Gonzo!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

771 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

10 Experts available now in Live!

Get 1:1 Help Now