note 38442 added to function.substr-count
| From: | XinfoXX at XXkarlXX-XXphilippXX dot XXdeX | Date: | Sun, 21 Dec 2003 18:27:00 +0000 |
| Subject: | note 38442 added to function.substr-count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-62296@lists.php.net to get a copy of this message | ||
Yet another reference to the "cgcgcgcgcgcgc" example posted by "chris at pecoraro dot
net":
Your request can be fulfilled with the Perl compatible regular expressions and their lookahead and
lookbehind features.
The example
$number_of_full_pattern = preg_match_all('/(cgc)/', "cgcgcgcgcgcgcg", $chunks);
works like the substr_count function. The variable $number_of_full_pattern has the value 3, because
the default behavior of Perl compatible regular expressions is to consume the characters of the
string subject that were matched by the (sub)pattern. That is, the pointer will be moved to the end
of the matched substring.
But we can use the lookahead feature that disables the moving of the pointer:
$number_of_full_pattern = preg_match_all('/(cg(?=c))/', "cgcgcgcgcgcgcg",
$chunks);
In this case the variable $number_of_full_pattern has the value 6.
Firstly a string "cg" will be matched and the pointer will be moved to the end of this
string. Then the regular expression looks ahead whether a 'c' can be matched. Despite of
the occurence of the character 'c' the pointer is not moved.
----
Manual Page -- http://www.php.net/manual/en/function.substr-count.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38442
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38442&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38442&report=yes
Search -- http://master.php.net/manage/user-notes.php