Jump to content

preg_match_all question


python72

Recommended Posts

I have noticed that if I run the preg_match_all function and use PREG_OFFSET_CAPTURE option to start capture somwhere in the middle of the string the second half of the string will be searched first returning the matching sections along with positions, then it goes up to the top half and returns matches from there too.

Is there way to parse only between start point and end of string?

Link to comment
Share on other sites

PREG_OFFSET_CAPTURE doesn't start in the middle of the string, it stores the offset, in the array, of each matched section.

 

You can pass the function an offset as it's fourth argument.  But, this is evaluated in bytes, and wouldn't be the same as passing it part of the string using the substr() function.

 

It is explained here

Link to comment
Share on other sites

Well, I guess I did not understand the PREG_OFFSET_CAPTURE. I am passing $offset as the fourth argument though and it seems that first preg_match_all starts at the $offset and goes to the end of the string, then goes to the start of the string and searches till the $offset position.

Is it the way it is supposed to happen or am I doing something wrong?

Link to comment
Share on other sites

You may need to pass part of the string to the function.  As the offset counts in bytes, which includes part of the pattern.

 

I would use:

$SetNumber = preg_match_all($Pattern,substr($Page,$NumberPosition),$Matches, PREG_OFFSET_CAPTURE);

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.