PHP preg_match_all problem

Posted on 2011-09-26
Medium Priority
Last Modified: 2012-06-27
What's wrong with this script...

I'm trying to extract URLs:




preg_match_all('/f=\"(.*)\.zip/', $output, $matches);
$num=count($matches); echo $num.'<br>';

for($run=0; $run<$num; $run++)
echo $matches[$run,0].' > '.$matches[$run,1].' > '.$matches[$run,2].'<br>';


Open in new window

I get this error:
Parse error: syntax error, unexpected ',', expecting ']' in ... on line 10

It's the same when I don't escape the = sign either.
Question by:ADFB
  • 2
LVL 10

Expert Comment

ID: 36600192
I didn't know of that notation " $matches[$run,0]" to recall

Try to modify your code as follows:

for($run=0; $run<$num; $run++)
echo $matches[$run][0].' > '.$matches[$run][1].' > '.$matches[$run][2].'<br>';

HTH Bye!

Accepted Solution

Rik-Legger earned 2000 total points
ID: 36600194
I tried to fix your script,
it had some flaws in it.



$output = file_get_contents($url);
//echo $output;

preg_match_all('/<a href=".*">(.*)<\/a>/', $output, $matches);
$num=count($matches[1]); echo $num.'<br>';

for($run=0; $run<$num; $run++) {
	echo $matches[1][$run] . '<br>';


Open in new window


Author Comment

ID: 36600256
I didn't know of that notation " $matches[$run,0]"
Ooops! That was my problem, from an old programming language I used to use.
LVL 10

Expert Comment

ID: 36600286
And so?

Rik's script will only output the FIRST line of matches.

Was that your intention?

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Today, unlike web development, the mobile landscape is complex enough for a software engineer and Android is posing more challenging environment thanks to its fragmentation issues on hardware and software fronts.
I recently worked on a Wordpress site that utilized the popular ContactForm7 (https://contactform7.com/) plug-in that only sends an email and does not save data. The client wanted the data saved to a custom CRM database. This is my solution.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Starting up a Project

586 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