Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8986825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:38:35+00:00 2026-06-15T21:38:35+00:00

I’ve used preg_match_all’s PREG_PATTERN_ORDER to return positions in the values found in a string.

  • 0

I’ve used preg_match_all’s PREG_PATTERN_ORDER to return positions in the values found in a string. Then when trying to substr, referencing them back, they don’t line up. I expected an offset, but there seems to be an undetermined misalignment per case.

Is this because preg_match_all is returning bytes and not characters? If so, is there a way to convert bytes to characters? If I’m completely off the mark I can post some code…

Okay here is the applicable code:

// RETURN POSITION OF START AND END TAGS TO ARRAY
function getTagPositions($strBody, $start, $end)
{
    preg_match_all('/' . preg_quote($start, '/') . '([\w\s.]*?)'. preg_quote($end, '/').'/im', $strBody, $strTag, PREG_PATTERN_ORDER);
    $intOffset = 0;
    $intIndex = 0;
    $intTagPositions = array();

    foreach($strTag[0] as $strFullTag) {
        $intTagPositions[$intIndex] = array('start' => (strpos($strBody, $strFullTag, $intOffset)), 'end' => (strpos($strBody, $strFullTag, $intOffset) + strlen($strFullTag)));
        $intOffset += strlen($strFullTag);
        $intIndex++;
    }
    return $intTagPositions;
}

function arrayValRecursive($key, array $arr){
    $val = array();
    array_walk_recursive($arr, function($v, $k) use($key, &$val){
        if($k == $key) array_push($val, $v);
    });
    return count($val) > 1 ? $val : array_pop($val);
}

$arrayOfPositions = getTagPositions($html,$go,$stop);
$arrayOfStart = arrayValRecursive('start', $arrayOfPositions);   //print_r($arrayOfStart);
$arrayOfEnd = arrayValRecursive('end', $arrayOfPositions);   //print_r($arrayOfEnd);

  $offset = 0;
  $range = $arrayOfStart[$i] + $offset;
  $rangeEnd = $arrayOfEnd[$i];    
  echo '<br>'.$range.' to '.$rangeEnd.' is: <br>'; 
  echo substr($html, $range, $rangeEnd); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:38:36+00:00Added an answer on June 15, 2026 at 9:38 pm

    According to preg_match_all

    Return Values
    Returns the number of full pattern matches (which might be zero), or FALSE if an error occurred.

    If you want to get offsets into the string, use PREG_OFFSET_CAPTURE

    PREG_OFFSET_CAPTURE
    If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.

    Here’s an example:

    $count = preg_match_all('/\S+/', 'Hello, world!', $matches, PREG_OFFSET_CAPTURE);
    echo "count=$count\n";
    var_dump($matches);
    

    This gives as output

    count=2
    array(1) {
      [0]=>
      array(2) {
        [0]=>
        array(2) {
          [0]=>
          string(6) "Hello,"
          [1]=>
          int(0)
        }
        [1]=>
        array(2) {
          [0]=>
          string(6) "world!"
          [1]=>
          int(7)
        }
      }
    }
    

    You can see the pattern matches $count=2 times. It matches “Hello,” at position $matches[0][0][1]=0 and it matches “world!” at position $matches[0][1][1]=7

    And here’s how you loop through all matches

    $start = array();
    $end = array();
    foreach($matches[0] as $match) {
        $start[] = $match[1];
        $end[] = $match[1] + strlen($match[0]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.