Link to home
Start Free TrialLog in
Avatar of lawrence_dev
lawrence_dev

asked on

Preg Match with for each statement for multiple records

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);
  
        $url=$matches1[$j];
	$productname=$matches1[2];
		
		
		
	$statement = $conn->prepare('INSERT IGNORE INTO urldownload (URL, Name) VALUES(:URL, :Name)');

		$params = array(':URL'=>$url,
						':Name'=>$productname);
		                $statement->execute($params); 
	

	
	}

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Maybe you want preg_match_all to get more than one match?
http://php.net/manual/en/function.preg-match-all.php

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.
http://php.net/manual/en/function.var-dump.php
http://php.net/manual/en/control-structures.foreach.php

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.
Avatar of lawrence_dev
lawrence_dev

ASKER

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

Please do not answer my questions!  Ignore them!

Thank You!
Please post the test data.  That's really the only thing we need to show you HOW!!
PLEASE DO NOT ANSWER MY QUESTIONS!  

So you will refer me to php.net, and they will give me complicated structure, but you have to have test data to give me structure???  I call BS!
Avatar of Terry Woods
@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.
Terry,
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 php.net, 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...
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 php.net is not a solution.
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
did I provide enough data for you to provide a response to my question
Yep, otherwise you wouldn't have the answer ;o)
THANKS GARY!!!!  SIMPLE TO UNDERSTAND!