aj123cd Posted August 7, 2012 Share Posted August 7, 2012 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 Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 7, 2012 Share Posted August 7, 2012 Use nl2br when you display it to convert new line characters to HTML line breaks. Quote Link to comment Share on other sites More sharing options...
aj123cd Posted August 7, 2012 Author Share Posted August 7, 2012 scootstah ; thank you. The method I am using is a good method or is there any better method? Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 7, 2012 Share Posted August 7, 2012 I don't understand your logic for the while loop, especially considering you are just replacing the $out variable each iteration. Also, I don't think it will work on nested quotes. Quote Link to comment Share on other sites More sharing options...
aj123cd Posted August 7, 2012 Author Share Posted August 7, 2012 I am building up forum website, When the user quote the message in the reply; I use to replace the [quote=user;date]content[/quote] to Posted by: user name on Date . Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 7, 2012 Share Posted August 7, 2012 I understand that, but that doesn't explain why you used a loop. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 7, 2012 Share Posted August 7, 2012 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 7, 2012 Share Posted August 7, 2012 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 7, 2012 Share Posted August 7, 2012 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; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 7, 2012 Share Posted August 7, 2012 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 Quote Link to comment Share on other sites More sharing options...
aj123cd Posted August 8, 2012 Author Share Posted August 8, 2012 Psycho scootstah Thank you for your HELP. Quote Link to comment Share on other sites More sharing options...
aj123cd Posted August 8, 2012 Author Share Posted August 8, 2012 $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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 8, 2012 Share Posted August 8, 2012 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'; Quote Link to comment Share on other sites More sharing options...
aj123cd Posted August 26, 2012 Author Share Posted August 26, 2012 THANK YOU ALL Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.