Forum Moderators: coopster

Message Too Old, No Replies

Reading through entire file

I got it to display the first match but not the whole thing

         

operafan

10:12 am on Mar 26, 2004 (gmt 0)

10+ Year Member



I've got this preg_match working
if (preg_match('/(text1)(.*)(text2)/', $data, $match))

After displaying the match in between which is match[2]

it does not read & display the other text in between match which is below the context of the file.

How do i get it to display the other matches below & then display it?

Thanks in advance

coopster

12:13 pm on Mar 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



preg_match() [php.net] will stop searching after the first match. preg_match_all() [php.net] on the contrary will continue until it reaches the end of subject...

operafan

6:54 am on Mar 27, 2004 (gmt 0)

10+ Year Member



Thanks, forgot about that one, but after applying it I get Array.. wonder what is that?

WhosAWhata

8:52 pm on Mar 27, 2004 (gmt 0)

10+ Year Member



it means that every instance of your text was stored in the same variable. to access one of the lines type $match[#];
ie: the first match is $match[0]; the second in $match[1];
to display them all try something like
print_r($match);
or
echo "<table>"; foreach($match as $key => $value){ echo "<tr><td>$key</td><td>$value</td></tr>"; }

operafan

1:19 am on Mar 29, 2004 (gmt 0)

10+ Year Member



Thanks so much whosawhoa, I'm still very raw in php programmin, so thanks for the tip.

operafan

4:29 am on Mar 29, 2004 (gmt 0)

10+ Year Member



Ok, so now I decided to go 1 step further. I have now duplicate results for each record pulled up from the method used.

So I tried using $value = array_unique($match);

But it still wouldn't filter & display unique records only - How do I get that function to work properly?

Thanks

Ok, finally got it to work. :)

WhosAWhata

5:55 am on Mar 29, 2004 (gmt 0)

10+ Year Member



can't exactly help you there, i'm really new to php as well and i just learned all about arrays a few weeks ago

i suppose you could do something like

echo "<table>";
foreach($match as $key => $value){
$display = 1;
foreach($match as $key2 => $value2) {
if (($value2 == $value) && ($key2!= $key)) { $display = 0; }
}
if ($display) {
echo "<tr><td>$key</td><td>$value</td></tr>";
}
}

i'm sure this isn't very efficient, but i suppose it should work