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 7721749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:00:13+00:00 2026-06-01T04:00:13+00:00

I am currently matching HTML using this code: preg_match(‘/<\/?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;/u’, $html, $match, PREG_OFFSET_CAPTURE, $position) It

  • 0

I am currently matching HTML using this code:

preg_match('/<\/?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;/u', $html, $match, PREG_OFFSET_CAPTURE, $position)

It matches everything perfect, however if I have a multibyte character, it counts it as 2 characters when giving back the position.

For example the returned $match array would give something like:

array
  0 => 
    array
      0 => string '<br />' (length=6)
      1 => int 132
  1 => 
    array
      0 => string 'br' (length=2)
      1 => int 133

The real number for the <br /> match is 128, but there are 4 multibyte characters, so it’s giving 132. I really thought adding the /u modifier would make it realize what’s going on, but no luck there.

  • 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-01T04:00:14+00:00Added an answer on June 1, 2026 at 4:00 am

    I looked at this suggestion from @Qtax:

    UTF-8 characters in preg_match_all (PHP)

    And for some more reference, this bug surfaced while using this:
    Truncate text containing HTML, ignoring tags

    The gist of the change is this:

    $orig_utf = 'UTF-8';
    $new_utf  = 'UTF-32';
    
    mb_regex_encoding( $new_utf );
    
    $html     = mb_convert_encoding( $html, $new_utf, $orig_utf );
    $end_char = mb_convert_encoding( $end_char, $new_utf, $orig_utf );
    
    
    mb_ereg_search_init( $html );
    
    $pattern = '</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;';
    $pattern = mb_convert_encoding( $pattern, $new_utf, $orig_utf );
    
    while ( $printed < $limit && $tag_match = mb_ereg_search_pos( $pattern, $html ) ) {
    
      $tag_position = $tag_match[0]/4;
      $tag_length   = $tag_match[1];
      $tag          = mb_substr( $html, $tag_position, $tag_length/4, $new_utf );
      $tag_name     = preg_replace( '/[\s<>\/]+/', '', $tag );
    
      // Print text leading up to the tag.
      $str = mb_substr($html, $position, $tag_position - $position, $new_utf );
    
      .......
    
    } 
    

    Also in reference to the truncate HTML page, there are other neccessary changes:

    $first_char = mb_substr( $tag, 0, 1, $new_utf );
    
    if ( $first_char == mb_convert_encoding( '&', $new_utf ) ) {
      ...
    }
    

    My text editor is UTF-8 so if I was comparing the 32 to my file’s ampersand, it wouldn’t work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently writing an HTML token recognizer in c++. I am using stacks
I'm currently using __PRETTY_FUNCTION__ when reporting messages (errors, warnings, etc) to a logfile. This
I'm looking to write / emulate a tab strip using HTML & CSS only.
I am using this for my password matching but it is not working can
I am using HTML and Jquery Below is the html code for TD which
I am currently working on a project where I a data matching algorithm needs
I'm currently learning lua. regarding pattern-matching in lua I found the following sentence in
Vim has support for matching pairs of curly brackets, parentheses, and square brackets. This
I'm using the following code (with the Underscore.js library) to add the attributes target=_blank
I currently have a program that finds and edits HTML files based on finding

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.