Solved

PHP preg_match_all problem

Posted on 2011-09-26
4
380 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 500 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

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

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…
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…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 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