Solved

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

Posted on 2015-02-24
3
62 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

808 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