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
https://forums.phpfreaks.com/topic/223702-preg_match_all-question/
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

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?

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.