I think working with PREG_OFFSET_CAPTURE
in this case only creates more work.
Demo of below scripts.
If the pattern only contains literal characters, then preg_
is overkill, just use mb_strpos()
and bear in mind that the returned value will be false
if the needle is not found in the haystack.
var_export(mb_strpos($str, 'H')); // 1
If you know that the needle will exist in the haystack, you can use preg_match_all()
with the marvellous \G
(continue) metacharacter and \X
(multibyte any character) metacharacter.
echo preg_match_all('/\G(?![A-Z])\X/u', $str); // 1
// if needle not found, will return the mb length of haystack
If you don't know if the needle will exist in the haystack, just check if the returned count is equal to the multibyte length of the input string.
$mbLength = preg_match_all('/\G(?![A-Z])\X/u', $str, $m);
var_export(mb_strlen($str) !== $mbLength ? $mbLength : 'not found');
But if you are going to call an extra mb_
function anyhow, then make just one match, check if a match was made, and measure its multibyte length if so.
var_export(
preg_match('/\X*?(?=[A-Z])/u', $str, $m) ? mb_strlen($m[0]) : 'not found'
);
All this said, I've never seen the need to count the multibyte position of something unless the greater task was to isolate or replace a substring. If this is the case, avoid this step entirely and just use preg_match()
or preg_replace()
to more directly serve your needs.