Solved

PHP preg_match_all problem

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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…
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.

739 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