Solved

PHP preg_match_all problem

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now