Preg Match with for each statement for multiple records

Posted on 2015-01-10
Last Modified: 2015-01-10
I am attempting to pull 20 urls and product names from each page.  I just need to fix this for each statement correctly to get more than 1 record on each page.

for ($j = 1; $j <= 1000; $j++) {

	preg_match('%<a href="(.*?)" class="catalogInvoiceLine">(.*?)<\/a>%',$buffer2,$matches1);
	$statement = $conn->prepare('INSERT IGNORE INTO urldownload (URL, Name) VALUES(:URL, :Name)');

		$params = array(':URL'=>$url,


Open in new window

Question by:lawrence_dev
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
  • 6
  • 2
  • 2
  • +1
LVL 110

Expert Comment

by:Ray Paseur
ID: 40542263
Maybe you want preg_match_all to get more than one match?

As you can see the function puts an array into the third argument.  You can see what is contained in this array with var_dump().  Once you know what you've got there, you can use foreach() to iterate over the array.

This is pretty much what you have to do every time you're working with a REGEX to extract subsets of data from a larger document.

The general design will go something like this...

1. preg_match_all($pattern, $document, $matches)
2. foreach ($matches[X] as $key => $val){ ... }

You will be able to discern an appropriate value for X after you see what you've got in the matches array.

If you want to post some test data, I'll be glad to show you a tested and working code example.

Author Comment

ID: 40542269
I just need to know HOW!!!  I do not links to and all of the test data BS.  You keep referring me to to read, but YOU wont show me how to do it!  Really makes no sense.  As I have said before, if I could understand, I would not be on this site!  

Please do not answer my questions!  Ignore them!

Thank You!
LVL 110

Expert Comment

by:Ray Paseur
ID: 40542275
Please post the test data.  That's really the only thing we need to show you HOW!!
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.


Author Comment

ID: 40542279

So you will refer me to, and they will give me complicated structure, but you have to have test data to give me structure???  I call BS!
LVL 35

Expert Comment

by:Terry Woods
ID: 40542388
@lawrence_dev, other experts won't want to help you when you're rude to those trying to help!

As Ray says, you need to change the preg_match to preg_match_all. He has even described how it should be done. If you'd like to offer an assurance that you'll be polite to the experts that are offering their help, I'd be willing to spend more time looking at your question.

Note that it's clear that you feel strongly that the way Ray is answering is not your cup of tea; it's ok if you want to decline his help, but please do so politely.

Author Comment

ID: 40542409
I really appreciate your help.  I have asked Ray politely several times to stop answering my questions - -politely!!  GaryC politely asked Ray to stop on one of my questions.  Evidently, Ray does not listen very well to requests.  It seems to be all about points.
When experts answer my questions 'simplified' like many experts do, I pick it up fast.  When you throw up complex solutions to simple requests about structure and how to do something, it confuses more than it helps.  (My issue is a great example why there are so many negative reviews of EE on Google...apparently I am not the only one with this issue.)
Some experts are great at simplifying the answer and answering the actual question.  Others like Ray pollute the water with links and complex solutions that many times really don't answer the question.

As I have stated many times, If I could understand, I would not pay for EE or need your help!

Furthermore, I left EE after several years mainly because of Ray.  I came back under a new alias to get a fresh start with the experts.  He still does not listen evidently...

Author Comment

ID: 40542553
I've requested that this question be deleted for the following reason:

These are not solutions.  These are links to other sites that I could access myself.  A link to is not a solution.
LVL 58

Accepted Solution

Gary earned 500 total points
ID: 40542532
// test data
$buffer2 = 'Some other text 
<a href="link1" class="catalogInvoiceLine">Link 1</a>
<a href="link2" class="catalogInvoiceLine">Link 2</a>
<a href="link3" class="catalogInvoiceLine">Link 3</a>';

$statement = $conn->prepare('INSERT IGNORE INTO urldownload (URL, Name) VALUES(:URL, :Name)');

preg_match_all('%<a href="(.*?)" class="catalogInvoiceLine">(.*?)<\/a>%',$buffer2,$matches1);

foreach($matches1[0] as $key=>$link){
	$params = array(':URL'=>$matches1[1][$key], ':Name'=>$matches1[2][$key]);

Open in new window

If you want to limit to a max of 20 url's when there may be more then just add to the start of the foreach

Open in new window


Author Comment

ID: 40542549
THANK YOU GARY!!!!!   WORKED GREAT!!!  I really appreciate your help!!   Just to clarify, did I provide enough data for you to provide a response to my question?
LVL 58

Expert Comment

ID: 40542552
did I provide enough data for you to provide a response to my question
Yep, otherwise you wouldn't have the answer ;o)

Author Closing Comment

ID: 40542554

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
can i read my emails on lamp ftp 4 70
PHP $_POST vs asp request 4 57
How do I allow for non standard CSV file 9 45
Link Stopped Working 7 32
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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
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.

734 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