Solved

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

Posted on 2015-02-24
3
63 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
[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 Comments
 
LVL 35

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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.

733 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