Doc #80977 [Com]: mb_ereg_search_pos is not multibyte-safe

From: Date: Thu, 22 Apr 2021 15:19:06 +0000
Subject: Doc #80977 [Com]: mb_ereg_search_pos is not multibyte-safe
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-18719@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80977&edit=1

 ID:                 80977
 Comment by:         v dot picture at free dot fr
 Reported by:        v dot picture at free dot fr
 Summary:            mb_ereg_search_pos is not multibyte-safe
 Status:             Not a bug
 Type:               Documentation Problem
 Package:            Regexps related
 Operating System:   Linux
 PHP Version:        8.0.3
 Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

So basically this function returns a position in number of bytes, not in number of characters.
How is this useful to anyone?
Also, if I want to know the actual position - in number of characters - of a match, how do I
proceed? I don't think there is any way to do that in PHP today.


Previous Comments:
------------------------------------------------------------------------
[2021-04-22 15:13:36] cmb@php.net

That is documented[1]:

| An array containing two elements. The first element is the
| offset, in bytes, where the match begins relative to the start of
| the search string, and the second element is the length in bytes
| of the match.

[1] <https://www.php.net/mb_ereg_search_pos>

------------------------------------------------------------------------
[2021-04-22 15:08:36] v dot picture at free dot fr

Description:
------------
Hello,
The mb_ereg_search_pos function is absolutely not multibyte-safe, it actually returns the position
of the match as if the string was not multibyte.

The results of this function are exactly the same as if you were using preg_match_all with
PREG_OFFSET_CAPTURE, even with the "unicode" flag it's simply NOT working.

Test script:
---------------
$string = 'jème lé ponés';
mb_ereg_search_init($string, '(?<=[ ^])\w+'); // Detect words
while ($pos = mb_ereg_search_pos()) {
    $match = mb_ereg_search_getregs()[0];
    $matchBasedOnPos = mb_substr($string, $pos[0], $pos[1]);
    if ($matchBasedOnPos !== $match) {
        throw new \LogicException("Match based on position '{$matchBasedOnPos}' does
not correspond to actual match '{$match}'");
    }
}

Expected result:
----------------
No exception thrown

Actual result:
--------------
LogicException: Match based on position 'é p' does not correspond to actual match
'lé'


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=80977&edit=1


Thread (4 messages)

« previous php.doc.bugs (#18719) next »