3

If a text has German Umlauts [äöü] the result of preg_match_all has wrong offsets (it seems each Umlaut extend the offset by 1)

I need the position of each word, because they will be replaced by other strings. With this tool https://regex101.com/r/UosqVD/2 it worked, the matches have the correct start value.

$pattern = "~\b\w+\b~u";
$text = "Käthe würde gerne wählen.";
if (preg_match_all($pattern, $text, $matches, PREG_OFFSET_CAPTURE)) {
    foreach ($matches[0] as $m) {
        echo $m[0]."; ".$m[1]."; ".mb_strlen($m[0], "utf-8")."<br />";
    }
}

Text; Start, Length<br>
Käthe; 0; 5<br>
würde; 7; 5<br>
gerne; 14; 5<br>
wählen; 20; 6<br>
Emma
  • 27,428
  • 11
  • 44
  • 69
Peter Gast
  • 33
  • 3

1 Answers1

2

The PHP documentation contains a function mb_preg_match_all() written by a user that seems to fit your needs :

function mb_preg_match_all($ps_pattern, $ps_subject, &$pa_matches, $pn_flags = PREG_PATTERN_ORDER, $pn_offset = 0, $ps_encoding = NULL) {
    // WARNING! - All this function does is to correct offsets, nothing else:
    //
    if (is_null($ps_encoding))
        $ps_encoding = mb_internal_encoding();

    $pn_offset = strlen(mb_substr($ps_subject, 0, $pn_offset, $ps_encoding));
    $ret = preg_match_all($ps_pattern, $ps_subject, $pa_matches, $pn_flags, $pn_offset);

    if ($ret && ($pn_flags & PREG_OFFSET_CAPTURE))
        foreach($pa_matches as &$ha_match)
            foreach($ha_match as &$ha_match)
                $ha_match[1] = mb_strlen(substr($ps_subject, 0, $ha_match[1]), $ps_encoding);
        //
        // (code is independent of PREG_PATTER_ORDER / PREG_SET_ORDER)

    return $ret;
}
Joffrey Schmitz
  • 2,393
  • 3
  • 19
  • 28