Page 1 of 1

preg_match_all() . on which line is the mach?

Posted: Tue Sep 12, 2006 10:47 am
by jmut
Hi,
I want to find out on which line a match found by preg_match_all() is located.
I made this snippet and wanted to ask if this is a good one...or it could be improved...like there is function for this
or something like this.

Code: Select all

<?php

$txt = 'Some sample text

match
dddd
matchdd';

preg_match_all("#match[d]*#",$txt,$matched,PREG_OFFSET_CAPTURE);

var_export($matched);
foreach ($matched[0] as $match)
{
    //$match[1]  - ofset position.
    $tmpTXT = substr($txt,0,$match[1]);
    $f = preg_split("#".PHP_EOL."#",$tmpTXT);
    echo "Match : ".$match[0]."\n";
    echo "Line: ". count($f)."\n";
}

?>
Using:
PHP 5.1.2