Jump to content

Quote function for my own personal forum


aj123cd

Recommended Posts

Have written a Quote function for my own personal forum

<?php 
$out = '[quote raja; 31st July 2012 4.27 AM ] 

My dear New Subject

■subject
■mySql
■PHP
■CSS
Hello I would like to write a littel story about your new life.

[/quote]
'; 
$cnt = 1; 
while($cnt != 0){     
$out = preg_replace('/\[quote\ (.*?);(.*?)\](.*?)\[\/quote\]/ms', '<blockquote>Posted by: \1 at \2.<br/>\3</blockquote>', $out, -1, $cnt); 
} 
echo $out;
?>

 

I am getting output like this..

 

Posted by: raja at 31st July 2012 4.27 AM .

My dear New Subject ?? subject ?? mySql ?? PHP ?? CSS Hello I would like to write a littel story about your new life.

 

 

I want the output like this

Posted by: raja at 31st July 2012 4.27 AM .

My dear New Subject

? subject

? mySql

? PHP

? CSS

Hello I would like to write a little story about your new life.

Can anyone advice me, what to do

Thank you.

 

Note - ?  is dote

Link to comment
Share on other sites

I understand that, but that doesn't explain why you used a loop.

 

It looks like he is using the loop to handle nested quotes. But I don't think it will work with the regex being used because of the lazy matches. The regex could use some work.

Link to comment
Share on other sites

I understand that, but that doesn't explain why you used a loop.

 

It looks like he is using the loop to handle nested quotes. But I don't think it will work with the regex being used because of the lazy matches. The regex could use some work.

 

I tested his code and it looks like you are right. And, nested quotes do work.

 

Neat solution, I've never seen it done that way.

Link to comment
Share on other sites

Yeah, I found that his regex did work for nested quotes, but I've reviewed the regex a few times and don't see how it is working correctly.

/\
(.*?)\[\/quote\]/ms'

 

The way I read that is that the part in bold would do a lazy match of all characters up till the first closing quote tag is found. So, the first opening quote tag would match content up till the first closing quote encountered - which would be from the last quote block. I guess I'm missing something. BUt, using those lazy quantifiers is kind of inefficient. Here is my rewrite that fixes the line breaks and removes the lazy quantifiers

 

function formatQuotes($input)
{
    $output = nl2br($input);
    $regEx  = '#\[quote\ ([^;]*);([^\]]*)\](.*)\[\/quote\]#ims';
    $format = '<blockquote>Posted by: \1 at \2.<br/>\3</blockquote>';
    do {
        $output = preg_replace($regEx, $format, $output, -1, $replaceCount);
    } while($replaceCount != 0);
    return $output;
}

Link to comment
Share on other sites

OK, I tested out his regex some more and my interpretation was correct. It is getting the correct results - but it is working by accident.

 

If this was the content to be parsed

    [_quote user 2; 31st July 2012 4.27 AM]

 

        [_quote user 1; 31st July 2012 4.27 AM]

        Quote from user #1

        [/_quote]

 

    Quote from user #2

    [/_quote]

 

The first iteration would produce this:

    <blockquote>Posted by: user 2 at31st July 2012 4.27 AM<br/>

 

        [_quote user 1; 31st July 2012 4.27 AM]

        Quote from user #1

        </blockquote>

 

    Quote from user #2

    [/_quote]

 

The regex I provided will handle each block as a whole

Link to comment
Share on other sites


$input='[quote user1;1] 
		[quote user0;0]some content here [/quote]
		[quote user1;1] this is my reply to user0 post[/quote] 
	some content here
	[/quote]
	[quote user1;1] this is my reply to user0 post[/quote]  
	[quote user0;0]some content here[/quote]';

 

Psycho, in you method I could not able to get the output I want for an above input.

Link to comment
Share on other sites

Psycho, in you method I could not able to get the output I want for an above input.

 

First off, don't just try something and provide a reply along the lines of "it doesn't work". Provide some details of what you tried, what were the expected results, and what were the actual results.

 

But, yes, you are correct. It doesn't work of nested and non-nested quotes together. In fact, I've never been able to build such a query that handles both. If you change to the non-greedy format you had previously for the content inside the quote tags it will work for that output. However, if there are any opening or closing quote tags that don't have a partner you will get some odd results.

 

Use this

$regEx  = '#\[quote\ ([^;]*);([^\]]*)\](.*?)\[\/quote\]#ims';

Link to comment
Share on other sites

  • 3 weeks later...
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.