preg_match_all() . on which line is the mach?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

preg_match_all() . on which line is the mach?

Post 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
Post Reply