Jump to content

[SOLVED] Match the last instance of


Glyde

Recommended Posts

I'm writing a tag parser (basically BBCode) for a project I'm developing.

 

Currently, to parse colors, I have been looking at something like this:

$postBody = preg_replace("@\[color=((#[0-9A-F]{3,6})|([A-Z\-]+))\](.+?)\[/color\]@is", "<span style='color: $1'>$4</span>", $postBody);

 

However, I want to be looking for the last index of [/color] in the code, not the first.  For example, I want:

[color=#CCC]test[color=#000]more[/color]text[/color]

At first glance, you would expect the "more" text to become #000, which it does.  But you would also expect "text" to be #CCC, but it's not.  It becomes #000.  Because preg_match searches for the next index of [/color] after finding , instead of searching for the last.  Is there any way to indicate that I want to search for the last index of something?  If not, I'll have to look for my alternative I guess which was using strpos and strrpos in conjunction.

Link to comment
https://forums.phpfreaks.com/topic/43827-solved-match-the-last-instance-of/
Share on other sites

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.