php remove all but last quoted reply in forum
Asked Answered
C

1

6

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]

Chafin answered 18/3, 2013 at 10:25 Comment(2)
[q] [q] ->[q] replace double with singleCannula
and what about ending [/q]? I think i need some kind of preg_replace function, but I'm bad at that..Chafin
R
0

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?

Ruffle answered 18/3, 2013 at 11:13 Comment(4)
You got mixed Q and P in your code, even tho I replced those, I still returns the same content.Chafin
Sorry, edited to correct. The first thing to check is that the values for the tag positions in the string are correct. If firstTag, secondTag, lastTag and secondLastTag point to the correct locations in the string then we at least know this bit is okay. Can you check this first?Ruffle
Try the code now, strrpos didn't seem to be working as documented with negative offsets, plus I didn't have it quite right anyway. This code seemed to work fine at sandbox.onlinephpfunctions.com now.Ruffle
Nicely done. Also you want to check if more than one [q] and [/q] is there with $count_quotes = substr_count($content,"[q]"); $count_endings = substr_count($content,"[/q]"); and then do it in the if -> if($count_quotes > 1 && $count_quotes==$count_endings) Thanks!Chafin

© 2022 - 2024 — McMap. All rights reserved.