[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

PHP preg_match_all problem

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

I'm trying to extract URLs:

<?php

$on='0';

$url='http://www.gutenberg.org/robot/harvest?offset='.$on.'&filetypes[]=txt&langs[]=en';
$output=file_get_contents($url);

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.
0
Comment
Question by:ADFB
[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
  • 2
4 Comments
 
LVL 10

Expert Comment

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

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!
0
 
LVL 8

Accepted Solution

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

<?php 

$on='0';

$url='http://www.gutenberg.org/robot/harvest?offset='.$on.'&filetypes[]=txt&langs[]=en';
$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

0
 

Author Comment

by:ADFB
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.
0
 
LVL 10

Expert Comment

by:ienaxxx
ID: 36600286
And so?

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

Was that your intention?
0

Featured Post

Industry Leaders: 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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…
Suggested Courses

650 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