Question

I'm creating my own forum and stuck with removing multiple quoted text from replies. I'll try to explain this with example.

Let's say we got first message with text Hello A.

Then somebody quotes this and we get: [q]Hello A[/q] Hello you too in database.

And if third person quotes second reply it goes more ugly and will be something like: [q] [q]Hello A[/q] Hello you too[/q] Hello both.

What I want do to is to remove all but the last quoted replies from quoted text. So in this case on third reply I want to strip [q]Hello A[/q] inside 3rd quote.

How to make it work on unlimited [q]'s?

edit: How to replace multiple [q]something[/q] inside the main [q] which is the first one? -> [q] [q]A[/q] B[/q] -> becomes -> [q]B[/q] OR [q][q][q]A[/q]B[/q]C[/q] -> becomes -> [q]C[/q]

Was it helpful?

Solution

If I've understood correctly, then you probably want something like this:

$firstTag = strpos($content, "[q]");
$lastTag = strrpos($content, "[/q]", 0);

$secondTag = strpos($content, "[q]", $firstTag + strlen("[q]"));
$secondLastTag = strrpos(substr($content, 0, $lastTag), "[/q]");

$content = substr_replace($content, "", $secondTag, $secondLastTag - $secondTag + strlen("[q]") + 1);

Apologies for any errors, I don't have a PHP interpreter handy to test with and it's been about 9 months since I used it so I'm a little rusty.

Effectively what we attempt to do though, is we first find the position in the string of the first opening tag, and we find the position of the last closing tag. Once we have these positions we can use them as offsets to start our searches to find the second opening tag and the second last closing tag. Once we know the positions of these, we then use substr_replace to replace all text in the content string starting from the second opening tag, to the second last closing tag with a blank string.

So to illustrate, if we have:

[q][q]Inner 3[q] Inner 2[/q] Inner 1[/q]Outer[/q]

we find the second [q] tag, the second last [/q] tag, and replace them and everything between them with a blank string and get:

[q]Outer[/q]

Is this what you were looking for?

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