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

  • Home
  • SEARCH
  • 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 6598657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:22:48+00:00 2026-05-25T18:22:48+00:00

I have stumbled upon a really odd bug with PHP’s preg_replace function and some

  • 0

I have stumbled upon a really odd bug with PHP’s preg_replace function and some regex patterns. What I’m trying to do is replace custom tags delimited by brackets and convert them to HTML. The regex has to account for custom “fill” tags that will stay with the outputted HTML so that it can be replaced on-the-fly when the page loads (replacing with a site-name for instance).

Each regex pattern will work by itself, but for some reason, some of them will exit the function early if preceded by one of the other patterns is checked first. When I stumbled upon this, I used preg_match and a foreach loop to check the patterns before moving on and would return the result if found – so hypothetically it would seem fresh to each pattern.

This didn’t work either.

Check Code:

function replaceLTags($originalString){
    $patterns = array(
                '#^\[l\]([^\s]+)\[/l\]$#i' => '<a href="$1">$1</a>',
                '#^\[l=([^\s]+)]([^\[]+)\[/l\]$#i'=> '<a href="$1">$2</a>',
                '#^\[l=([^\s]+) title=([^\[]+)]([^\[]+)\[/l\]$#i' => '<a href="$1" title="$2">$3</a>',
                '#^\[l=([^\s]+) rel=([^\[]+)]([^\[]+)\[/l\]$#i' => '<a href="$1" rel="$2">$3</a>',
                '#^\[l=([^\s]+) onClick=([^\[]+)]([^\[]+)\[/l\]$#i' => '<a href="$1" onClick="$2">$3</a>',
                '#^\[l=([^\s]+) style=([^\[]+)]([^\[]+)\[/l\]$#i' => '<a href="$1" style="$2">$3</a>',
                '#^\[l=([^\s]+) onClick=([^\[]+) style=([^\[]+)]([^\[]+)\[/l\]$#i' => '<a href="$1" onClick="$2" style="$3">$4</a>',
                '#^\[l=([^\s]+) class=([^\[]+) style=([^\[]+)]([^\[]+)\[/l\]$#i' => '<a href="$1" class="$2" style="$3">$4</a>',
                '#^\[l=([^\s]+) class=([^\[]+) rel=([^\[]+)] target=([^\[]+)]([^\[]+)\[/l\]$#i' => '<a href="$1" class="$2" rel="$3" target="$4">$5</a>'
            );

    foreach ($patterns as $pattern => $replace){
        if (preg_match($pattern, $originalString)){
            return preg_replace($pattern, $replace, $originalString);
        }
    }
}

$string = '[l=[site_url]/site-category/ class=hello rel=nofollow target=_blank]Hello there[/l]';

echo $alteredString = $format->replaceLTags($string);

The above “String” would come out as:

<a href="[site_url">/site-category/ class=hello rel=nofollow target=_blank]Hello there</a>

When it should come out as:

<a href="[site_url]/site-category/" class="hello" rel="nofollow" target="_blank">Hello there</a>

But if moved that pattern further up in the list to be checked sooner, it’d format correctly.

I’m stumped, because it seems like the string is being overwritten somehow every time it’s checked even though that makes no sense.

  • 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-05-25T18:22:49+00:00Added an answer on May 25, 2026 at 6:22 pm

    Seems to me you’re doing a lot more work than you need to. Instead of using a separate regex/replacement for each possible list of attributes, why not use preg_replace_callback to process the attributes in a separate step? For example:

    function replaceLTags($originalString){
      return preg_replace_callback('#\[l=((?>[^\s\[\]]+|\[site_url\])+)([^\]]*)\](.*?)\[/l\]#',
                                   replaceWithinTags, $originalString);
    }
    
    function replaceWithinTags($groups){
      return '<a href="' . $groups[1] . '"' . 
             preg_replace('#(\s+\w+)=(\S+)#', '$1="$2"', $groups[2]) .
             '>' . $groups[3] . '</a>';
    }
    

    See a complete demo here (updated; see comments).

    Here’s an updated version of the code based on new information that was provided in the comments:

    function replaceLTags($originalString){
      return preg_replace_callback('#\[l=((?>[^\s\[\]]+|\[\w+\])+)([^\]]*)\](.*?)\[/l\]#',
                                   replaceWithinTags, $originalString);
    }
    
    function replaceWithinTags($groups){
      return '<a href="' . $groups[1] . '"' . 
             preg_replace(
                 '#(\s+[^\s=]+)\s*=\s*([^\s=]+(?>\s+[^\s=]+)*(?!\s*=))#',
                 '$1="$2"', $groups[2]) .
             '>' . $groups[3] . '</a>';
    }
    

    demo

    In the first regex I changed [site_url] to \[\w+\] so it can match any custom fill tag.

    Here’s a breakdown of the second regex:

    (\s+[^\s=]+)   # the attribute name and its leading whitespace
    \s*=\s*
    (
      [^\s=]+   # the first word of the attribute value
      (?>\s+[^\s=]+)*  # the second and subsequent words, if any
      (?!\s*=)  # prevents the group above from consuming tag names
    )
    

    The trickiest part is matching multi-word attribute values. (?>\s+[^\s=]+)* will always consume the next tag name if there is one, but the lookahead forces it to backtrack. Normally it would only back off one character at a time, but the atomic group effectively forces it to backtrack by whole words or not at all.

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

Sidebar

Related Questions

I'm trying my first very basic Facebook app using php and have stumbled upon
I stumbled upon this site http://nathanborror.com/ and really loved the way they have done
Some of you might have stumbled upon this cute article - http://igoro.com/archive/quicksort-killer/ \ What
Greets, I'm currently working on a website and have stumbled upon some layout difficulties.
I have stumbled upon a really annoying situation: I am using Hibernate & Spring
I have stumbled upon a bug in Safari on iPad. $('#next_proj a').trigger('click'); .. does
I have stumbled upon the following F77 yacc grammar: http://yaxx.cvs.sourceforge.net/viewvc/yaxx/yaxx/fortran/fortran.y?revision=1.3&view=markup . How can I
I'm revisiting som old code of mine and have stumbled upon a method for
I'm mucking about with jQuery AJAX and have stumbled upon an oddity (or rather
I have recently stumbled upon a problem with selecting relationship details from a 1

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.