little PHP / regex help

Open discussion about any topic, as long as you abide by the rules of course!
Post Reply
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

little PHP / regex help

Post 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>";
Last edited by dmmh on Mon Dec 05, 2005 7:31 pm, edited 1 time in total.
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

any takers? :D
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Post 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;
}
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

why thank you :)
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
bitWISE
Posts: 10704
Joined: Wed Dec 08, 1999 8:00 am

Post by bitWISE »

I like the forum design man.
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post 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
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
saturn
Posts: 4334
Joined: Mon Mar 06, 2000 8:00 am
Location: The Netherlands

Post by saturn »

do your homework :p
fuck off sigs
[img]http://farm1.static.flickr.com/110/316626399_65f19bc409_t.jpg[/img]
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

why re-invent the wheel? :p
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
inolen
Posts: 707
Joined: Thu Dec 16, 1999 8:00 am

Post 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
bitWISE
Posts: 10704
Joined: Wed Dec 08, 1999 8:00 am

Post by bitWISE »

I have a really cool idea to help with moderation of a large forum. Message me sometime if you're interested.
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

will do, thx :D
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post 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
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
inolen
Posts: 707
Joined: Thu Dec 16, 1999 8:00 am

Post 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?
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post 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
Last edited by dmmh on Fri Dec 09, 2005 11:41 pm, edited 1 time in total.
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post 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
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
Post Reply