[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2015-02-24
3
Medium Priority
?
70 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 600 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 1400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

649 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