Question

I am writing a message board that allows for quoting and quote chaining of other users. The message board allows certain html tags including the <img>. When a message is quoted, I want to find the <img> and replace the picture with a thumbnail. Here is my current code:

<?php
 $pattern = "<quote msgid=\"t,(\d+),(\d+)@(\d+)\" from=\"([A-z0-9_\.-]+)\" posted=\"(\d+)\">(.+)<img src=\"https?:\/\/i\.(minus|imgur)\.com\/([A-z0-9_\.-]+)\.(jpg|gif|png|jpeg)\"( \/)?>";
 $replace = "<div class=\"quoted-message\" msgid=\"t,$1,$2@$3\"><div class=\"message-top\">From: $4 | Posted: $5</div>$6<img src=\"http:\/\/$7.com\/$8s\.$9\" />";
 $encoded = preg_replace($pattern, $replace, $encoded);
?>

The pattern itself works but the problem I am running into is that it does not play nice with nested quotes. For example the data can look like this:

<quote msgid="t,1234,3456@0" from"user" posted="test">Some words here<quote msgid="t,5635,57456@0" from"user2" posted="test">Some more <b>words</b> here<quote msgid="t,1243532,3452@0" from"user" posted="test">Something else here<img src="linktoimage"/></quote>some words can go here</quote>or here</quote>

My current regex will replace the first 3 Opening quote tags with one div tag instead of just matching the most inner tag. I know it has to do with the (.+) but I don't know what else I can do in order to check for preceding data.

Any help would be greatly appreciated.

Was it helpful?

Solution

Regular expressions are not very suitable for your situation (very oddly explained here). You should really use a parser.

OTHER TIPS

(?!<quote.*?<quote)(<quote)

Matches the inner most quote tag.

http://regexr.com?30ng2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top