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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.