質問: off 何か案は?
回答: ネクロポスティングはすみませんが、誰かが役に立つかもしれません:以下のコードは、preg_match関数とpreg_match_all関数の両方として機能し、utf8エンコードされた文字列のcorrect offsetで正しい一致を返します。 mb_internal_encoding('utf-8'); /** * returns array of matches in same format as preg_match or preg_match_all * @param bool $matchall if true, execute preg_match_all, otherwise preg_match * @param string $pattern the pattern to search for, as a string. * @param string $subject the input string. * @param int $offset the place from which to start the search (in bytes). * @return array */ function pregmatchcapture($matchall, $pattern, $subject, $offset = 0) { $matchinfo = array(); $method = 'preg_match'; $flag = preg_offset_capture; if ($matchall) { $method .= '_all'; } $n = $method($pattern, $subject, $matchinfo, $flag, $offset); $result = array(); if ($n !== 0 && !empty($matchinfo)) { if (!$matchall) { $matchinfo = array($matchinfo); } foreach ($matchinfo as $matches) { $positions = array(); foreach ($matches as $match) { $matchedtext = $match[0]; $matchedlength = $match[1]; $positions[] = array( $matchedtext, mb_strlen(mb_strcut($subject, 0, $matchedlength)) ); } $result[] = $positions; } if (!$matchall) { $result = $result[0]; } } return $result; } $s1 = 'попробуем русскую строку для теста'; $s2 = 'try english string for test'; var_dump(pregmatchcapture(true, '/обу/', $s1)); var_dump(pregmatchcapture(false, '/обу/', $s1)); var_dump(pregmatchcapture(true, '/lish/', $s2)); var_dump(pregmatchcapture(false, '/lish/', $s2)); 私の例の出力: array(1) { [0]=> array(1) { [0]=> array(2) { [0]=> string(6) "обу" [1]=> int(4) } } } array(1) { [0]=> array(2) { [0]=> string(6) "обу" [1]=> int(4) } } array(1) { [0]=> array(1) { [0]=> array(2) { [0]=> string(4) "lish" [1]=> int(7) } } } array(1) { [0]=> array(2) { [0]=> string(4) "lish" [1]=> int(7) } }