Page 1 of 1

little PHP / regex help

Posted: Mon Dec 05, 2005 7:25 pm
by dmmh
lo folks

I kinda suck with regular expressions and am seeking help fixing forum posts with multiple quoted content :S

how it looks now:

Image

my code:

$patterns[29]="#\[quote\](.*?)\[/quote\]#si";
$replacements[29]= "<span class=\"quote\">\\1</span>";

Posted: Mon Dec 05, 2005 7:30 pm
by dmmh
any takers? :D

Posted: Mon Dec 05, 2005 7:53 pm
by ^misantropia^
From the torrentbits.org source (which does roughly what you want):

Code: Select all

function format_quotes($s)
{
  while ($old_s != $s)
  {
  	$old_s = $s;

	  //find first occurrence of [/quote]
	  $close = strpos($s, "[/quote]");
	  if ($close === false)
	  	return $s;

	  //find last [quote] before first [/quote]
	  //note that there is no check for correct syntax
	  $open = _strlastpos(substr($s,0,$close), "[quote");
	  if ($open === false)
	    return $s;

	  $quote = substr($s,$open,$close - $open + 8);

	  //[quote]Text[/quote]
	  $quote = preg_replace(
	    "/\[quote\]\s*((\s|.)+?)\s*\[\/quote\]\s*/i",
	    "<p class=sub><b>Quote:</b></p><table class=main border=1 cellspacing=0 cellpadding=10><tr><td style='border: 1px black dotted'>\\1</td></tr></table><br>", $quote);

	  //[quote=Author]Text[/quote]
	  $quote = preg_replace(
	    "/\[quote=(.+?)\]\s*((\s|.)+?)\s*\[\/quote\]\s*/i",
	    "<p class=sub><b>\\1 wrote:</b></p><table class=main border=1 cellspacing=0 cellpadding=10><tr><td style='border: 1px black dotted'>\\2</td></tr></table><br>", $quote);
         

	  $s = substr($s,0,$open) . $quote . substr($s,$close + 8);
  }

	return $s;
}

Posted: Mon Dec 05, 2005 8:12 pm
by dmmh
why thank you :)

Posted: Mon Dec 05, 2005 8:27 pm
by bitWISE
I like the forum design man.

Posted: Mon Dec 05, 2005 8:28 pm
by dmmh
thanks man
took me quite some time to tweak the CSS etc, so I am also quite fond of it, but glad to hear Im not the only one :D

Posted: Mon Dec 05, 2005 8:29 pm
by saturn
do your homework :p

Posted: Mon Dec 05, 2005 8:32 pm
by dmmh
why re-invent the wheel? :p

Posted: Mon Dec 05, 2005 8:35 pm
by inolen
dmmh wrote:why re-invent the wheel? :p
Because that's a somewhat common regex problem you should learn how to get around :P

Posted: Mon Dec 05, 2005 8:38 pm
by bitWISE
I have a really cool idea to help with moderation of a large forum. Message me sometime if you're interested.

Posted: Mon Dec 05, 2005 9:23 pm
by dmmh
will do, thx :D

Posted: Fri Dec 09, 2005 10:47 pm
by dmmh
inolen wrote:
dmmh wrote:why re-invent the wheel? :p
Because that's a somewhat common regex problem you should learn how to get around :P
i fuckin despise regex, the syntax is just so wickedly weird :p

Posted: Fri Dec 09, 2005 11:34 pm
by inolen
dmmh wrote:
inolen wrote:
dmmh wrote:why re-invent the wheel? :p
Because that's a somewhat common regex problem you should learn how to get around :P
i fuckin despise regex, the syntax is just so wickedly weird :p
Like?

Posted: Fri Dec 09, 2005 11:35 pm
by dmmh
needs some tweaking and now my smilies are borked, but Ill find this shortly :D

thx

edit: had to write the _strlastpos() function myself though, since the source isnt free :(
but works (y)

Image

Posted: Fri Dec 09, 2005 11:35 pm
by dmmh
inolen wrote:
dmmh wrote:
inolen wrote: Because that's a somewhat common regex problem you should learn how to get around :P
i fuckin despise regex, the syntax is just so wickedly weird :p
Like?
dunno, it just doesnt make much sense to me :D