Home  |  Linux  | Mysql  | PHP  | XML
From:MonkeyMan@osu1.php.net Date:Tue Oct  7 02:25:53 2008
Subject:note 86182 added to function.preg-match-all
Here is a way to match everything on the page, performing an action for each match as you go. I had used this idiom in other languages, where its use is customary, but in PHP it seems to be not quite as common.

<?php
function custom_preg_match_all($pattern, $subject)
{
	$offset = 0;
	$match_count = 0;
	while(preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, $offset))
	{
		// Increment counter
		$match_count++;
	
		// Get byte offset and byte length (assuming single byte encoded)
		$match_start = $matches[0][1];
		$match_length = strlen(matches[0][0]);

		// (Optional) Transform $matches to the format it is usually set as (without PREG_OFFSET_CAPTURE set)
		foreach($matches as $k => $match) $newmatches[$k] = $match[0];
		$matches = $new_matches;
	
		// Your code here
		echo "Match number $match_count, at byte offset $match_start, $match_length bytes long: ".$matches[0]."\r\n";
			
		// Update offset to the end of the match
		$offset = $match_start + $match_length;
	}

	return $match_count;
}
?>

Note that the offsets returned are byte values (not necessarily number of characters) so you'll have to make sure the data is single-byte encoded. (Or have a look at paolo mosna's strByte function on the strlen manual page).
I'd be interested to know how this method performs speedwise against using preg_match_all and then recursing through the results.
----
Server IP: 203.88.112.190
Probable Submitter: 58.96.34.67
----
Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php
Edit        -- https://master.php.net/note/edit/86182
Del: integrated  -- https://master.php.net/note/delete/86182/integrated
Del: useless     -- https://master.php.net/note/delete/86182/useless
Del: bad code    -- https://master.php.net/note/delete/86182/bad+code
Del: spam        -- https://master.php.net/note/delete/86182/spam
Del: non-english -- https://master.php.net/note/delete/86182/non-english
Del: in docs     -- https://master.php.net/note/delete/86182/in+docs
Del: other reasons-- https://master.php.net/note/delete/86182
Reject      -- https://master.php.net/note/reject/86182
Search      -- https://master.php.net/manage/user-notes.php

Navigate in group php.notes at sever news.php.net
Previous Next




  
© No Copyright
You are free to use Anything
Site Maintained by PHP Developer
Powered By PHP Consultants