[SOLVED] BBCode!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

now you've got a more fun infinite loop.

Code: Select all

[/quote][quote]
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Damn, damn, damn!

Code: Select all

while (preg_match("'@\[quote=(.*?)\](.*?)\[/quote\]@ims'",$text)!=0){
?

EDIT: That doesn't even work...:(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you've got some boo boo's in your regex. ;)

Code: Select all

<?php

ob_start();

?>
[quote=test1]test1[/quote]
[quote=test2
]test2[/quote]
[quote=test3]
test3
[/quote]test4
[/quote]test4[quote=]
test5[/quote]
<?php

$t = ob_get_clean();

preg_match_all("'@\[quote=(.*?)\](.*?)\[/quote\]@ims'",$t,$m);	//	<-- yours

var_export($m);

preg_match_all('@\[quote=(.*?)\](.*?)\[/quote\]@ims',$t,$m);

var_export($m);

?>

Code: Select all

array (
  0 =>
  array (
  ),
  1 =>
  array (
  ),
  2 =>
  array (
  ),
)

array (
  0 =>
  array (
    0 => '[quote=test1]test1[/quote]',
    1 => '[quote=test2
]test2[/quote]',
    2 => '[quote=test3]
test3
[/quote]',
    3 => '[quote=]
test5[/quote]',
  ),
  1 =>
  array (
    0 => 'test1',
    1 => 'test2
',
    2 => 'test3',
    3 => '',
  ),
  2 =>
  array (
    0 => 'test1',
    1 => 'test2',
    2 => '
test3
',
    3 => '
test5',
  ),
)
although using modifier m and s seems a bit odd. s should suffice.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Yup, that did it. Thanks feyd!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

evilmonkey wrote:
jshpro2 wrote:Yeah your while loop should be checking for the same text it is going to replace (regex), also this is a good place for a do..while loop

while loop for simple quote
check string
replace text
check string

do..while loop for simple quote
replace text
check string
I don't think do while is good for this case...why run the memory intensive regex function if there's no
tag at all?
You run a regex no matter what on your loop..

With the do while it runs a replacement so for a post with only one single
foo
only two regexes get run, the replacement and the check, whereas in a while loop it checks to see if there is a quote, then replaces it, then checks again to make sure it got them all, a total of 3 checks. The do..while is more efficient.
Post Reply