Php removes everything except the last quoted answer in the forum

I create my own forum and fixated on deleting several quotes from the answers. I will try to explain this with an example.

Say we got the first message with the text Hello A.

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

And if the third person quotes the second answer, it looks uglier and will look something like this: [q] [q]Hello A[/q] Hello you too[/q] Hello both .

What I want to do is to remove everything except the last quoted answers from the quoted text. So, in this case, on the third answer, I want to split [q]Hello A[/q] inside the third quote.

How to make it work without restrictions [q]?

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

+6
source share
1 answer

If I 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); 

I apologize for any errors, I do not have a PHP interpreter with which to test, and it has been about 9 months since I used it, so I'm a little rusty.

Effectively, what we are trying to do, we first find the position in the line of the first opening tag, and we find the position of the last closing tag. After we have these positions, we can use them as offsets to start the search to find the second opening tag and the second last closing tag. Once we know their positions, we then use substr_replace to replace all the text in the content line, starting from the second opening tag, to the second last closing tag with an empty string.

So, to illustrate, if we have:

[q] [q] Internal 3 [q] Internal 2 [/ q] Internal 1 [/ q] External [/ q]

we will find the second tag [q], the second last tag [/ q] and replace them and everything in between with an empty string and get:

[Q] External [/ Q]

Is this what you were looking for?

+1
source

All Articles