Re: Does the array parameter of ereg actually return strings matched?

From: Date: Fri, 11 May 2001 18:45:28 +0000
Subject: Re: Does the array parameter of ereg actually return strings matched?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-52498@lists.php.net to get a copy of this message
On Wed, May 09, 2001 at 06:13:17PM -0700, Dexter wrote: > Does the array parameter of ereg actually return strings matched? I > tried to read a file looking for a string using ereg and wanted to put > the matched strings found in an array. Doesn't seem to be happening > with the following. > > $contents=fread($readfile,100000); > $pattern="<a href.*\">"; > $i=0; > while(!feof($readfile)) > { > $contents=fgets($readfile,4065); > if(eregi($pattern,$contents,$regs)) > { > echo "$regs[$i]<br>"; > } > $i++; > } Yes, it actually does return strings matched. I would not expect your code, as you typed it, to do what you expect. I realigned things to make it easier to see what is happening. With what you have, it is very unlikely that you will ever print out a match. You are incrementing your counter for every line of the file, not for every match actually found. Try this code instead: <?php $contents=fread($readfile,100000); $pattern="<a href.*\">"; while(!feof($readfile)) { $contents=fgets($readfile,4065); if(eregi($pattern,$contents,$regs)) { for($i = 0; $i < sizeof($regs); $i++) { echo "$regs[$i]<br>"; } } } ?> -- Jason Stechschulte jpstech@unoh.edu -- I hope I'm not getting so famous that I can't think out load [sic] anymore. -- Larry Wall in <199709032332.QAA21669@wall.org>

« previous php.general (#52498) next »