Forum Moderators: coopster
-----MMMMM------IIIII----MMM---OOOO---I---MMMM-----
Is there any way I can gather and group my information and get, for example:
6-10:M
17-21:I
26-28:M
32-35:O
39:I
43-46:M
//$subject = '-----MMMMM------IIIII----MMM---OOOO---I---MMMM-----';
//$pattern = '/[a-z]+/i'; // Works fine on the original subject string
$subject = '-----MMMMM------IIIII----MMM---OOOO---I---MMMM-----XXZZXX---MMMM';
//$pattern = '/[a-z]+/i'; // Fails in this case
$pattern = '/\b((\w)\2+¦\w)(?:\b)/';
preg_match_all(
$pattern,
$subject,
$matches,
PREG_SET_ORDER ¦ PREG_OFFSET_CAPTURE
);
print "$subject\n";
print str_repeat('1234567890', 7) . "\n";
print_r($matches) . "\n";
foreach ($matches as $match) {
$length = strlen($match[0][0]);
$start = $match[0][1] + 1; // adjust for zero-based indexing
$end = $match[0][1] + $length;
print "$start - $end:" . $match[0][0][0] . "\n";
}
The other printing code in the middle was left there so you could analyze how the patterns are captured and how the offsets work. Details are on the manual pages in the link.
Note: The forum breaks the pipe symbol so you must rekey it if you copy/paste the code