Here's a weird wrench that I'm dealing with at the moment. I've been back and forth with it, using a variety of regular expressions, spacing and quote variations but there seems to only be an issue with the placement of the closing </a>
tag.
I've built a RegEx parser (I know) to grab tags from HTML output and trigger custom closures for each from a library of what we call "shortcodes". It'll just swap out the tag for the closure's return value, and if the closure doesn't exist, leave the tag in place.
When that </a>
is placed on the same line as the tag I'm grabbing, it causes preg_replace_callback
to fail. When I add a line break, everything works flawlessly. We're using these tags all over the place as a central part of our template system and they're working wonderfully in every other instance.
So my question is... why?! Is this a bug or am I the bug?
EDIT: Updated pattern to limit reluctancy. Closing </a>
tag still kills the process. (@Alan Moore)
EDIT 2: So, the regex was the culprit. It was pulling in the content beyond the tag on the same line on some cases, but this pattern solved the problem - '#\[\s*(\w[^\]\s]*)(.*?)\s*\]#i'
.
Failing HTML
<div>
<a href="[ make_url post_id='200' ]">Some Link</a>
</div>
Successful HTML
<div>
<a href="[ make_url post_id='200' ]">
Some Link
</a>
</div>
or
<div>
<a href="[ make_url post_id='200' ]">Some Link
</a>
</div>
PHP
Nothing fancy, and even when I dumb it down to the basics it still fails.
$content = 'HTML output from an output buffer';
//$old_pattern = '#\[\s*?(\w+?(?:\.?\w+?)*?(?=\s|\]))(.+)*?\s*?\]#i';
$pattern = '#\[\s*(\w+(?:\.?\w+)*(?=\s|\]))(.+)\s*\]#i';
$content = preg_replace_callback( $pattern, function( $matches ) {
// Do things with the matches
}, $content );
// Send content back to the template
return $content;