Jump to content

PREG_OFFSET_CAPTURE returns wrong offset if there is a multibyte character


moxol

Recommended Posts

Function preg_match_all($pattern, $subject, PREG_OFFSET_CAPTURE ) returns wrong offset for matched value if there are multibyte characters in subject.
It seems that PHP counts multibyte character as two characters instead of one.

Is there a solution to this problem?

Link to comment
Share on other sites

Is that what you want to do? Find out the "character" position for a match's byte position?

You're only giving out small pieces of information at a time. It's hard to give advice on broader problems or situations when we have to work with is your use of mb_strlen and substr...

Link to comment
Share on other sites

 

36 minutes ago, requinix said:

Is that what you want to do? Find out the "character" position for a match's byte position?

Yes, I am using regex preg_match_all to find offset of matches so that I could use substr to the position of PREG_OFFSET_CAPTURE.
And then I would use mb_strlen(substr($text, 0, $offset_capture))
to find "real" offset if there are multibytes in the text.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.