How to display the found matches of preg_match function in PHP?

Posted by Eric on Stack Overflow See other posts from Stack Overflow or by Eric
Published on 2011-01-11T04:48:10Z Indexed on 2011/01/11 4:53 UTC
Read the original article Hit count: 176

Filed under:

I am using the following to check if links exist on file.php:

$fopen = fopen('file.php', 'r');

$fread = fread($fopen, filesize('file.php'));

$pattern = "/^<a href=/i";

if (preg_match($pattern, $fread)) {
    echo 'Match Found';
} else {
    echo 'Match Not Found';
}

if I echo preg_match($pattern, $fread) I get a boolean value, not the found matches. I tried what was on the php.net manual and did this:

preg_match($pattern, $fread, $matches);

then when I echoed $matches I got "Array" message. So I tried a foreach loop and when that didn't display anything I tried $matches[0] and that too outputted nothing.

So how does one go about displaying the matches found?

© Stack Overflow or respective owner

Related posts about php