You may use preg_match (or preg_match_all to get multiple matches) with the PREG_OFFSET_CAPTURE argument:
$str = "example ## string";
if (preg_match('~#{1,6}~', $str, $m, PREG_OFFSET_CAPTURE)) {
print_r($m);
}
See PHP preg_match reference:
PREG_OFFSET_CAPTURE
If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
See the PHP demo, output:
Array
(
[0] => Array
(
[0] => ##
[1] => 8
)
)
strposwill give you the position.gmodifier in PHP regex, andmis not necessary here since there is no^and$.