Forum Moderators: coopster

Message Too Old, No Replies

ereg_match_all

         

MediumDave

3:42 pm on Apr 26, 2005 (gmt 0)

10+ Year Member



Is there any way of extracting the position of each expression within the text?

I am using ereg_match_all to match an expression in an HTML file, but would like to know the position of each occurence within the file. Is this possible?

Thanks.

timster

5:58 pm on Apr 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe overlong, but would this do?

<?php
$text = "Who is it who? Who is it? What is it?";
$word = "it";

$total_length = 0;
preg_match_all("/(.*?)\b($word)\b/s", $text, $my_matches);

foreach ($my_matches[0] as $a_match) {

$length += strlen($a_match);
echo $length - strlen($word) . "<br>\n";
}
?>