
June 30th, 2006
10:35 AM
Pawn Coder
Status: Offline!
Returning # of matches
Here's the function I have:
<?php
function strip_quote_tags($string , $return_matches = FALSE)
{
$pattern = '`\[quote=\"([a-z0-9]+)\"\](.+?)\[/quote\]`is';
if($return_matches)
{
preg_match_all($pattern , $string , $matches);
return count($matches);
}
$string = preg_replace($pattern , '' , $string);
return $string;
}
?>
Here's how I'm testing it:
<?php
include("../functions.php");
$string = '[quote="blah"][quote="blah"]blah[/quote]blah[/quote][quote="blah"]blah[/quote][quote="blah"]blah[/quote]';
echo 'Matches: ' . strip_quote_tags($string , TRUE);
?>
It returns 3, but doesn't include the nested one.
So my question is: How do I return the total # of matches including the nested ones?
Thanks in advance!
---brandon
___________________
- Moderator at the official AMX Mod X forums

July 2nd, 2006
04:19 PM
Neversidian
Status: Offline!
you need a recursive regex to do that, or to not use regex at all.
___________________
Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!

July 3rd, 2006
11:29 AM
Pawn Coder
Status: Offline!
Could you give me an example, please?
---brandon
___________________
- Moderator at the official AMX Mod X forums

July 3rd, 2006
12:27 PM
Neversidian
Status: Offline!
I really dont want to write the code for you. Basically just go use strpos to find the first {quote} and then go until you find the {/quote}, if you find another {quote} before then set the function to know that it must find 2 {/quotes}.
These are all workaround to what I believe is the best method of doing bbtags. Here is a brief rundown to how it works.
basically you validate all of the tags on submission, then with the valid tags you put a unique hash on the bbtags. Something like {quote=person:di8eif9812kfloe7}stuff{/quote:di8eif9812kfloe7} then to actually replace them all you have to do is str_replace any of the bbtags that have the unique hash in them, and obviously you store the unique hash for each post.
___________________
Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!

July 3rd, 2006
12:28 PM
Neversidian
Status: Offline!
I used {'s so the forum wouldnt try to parse the [quote]'s
___________________
Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!

July 4th, 2006
12:09 PM
Neverside Newbie
Status: Offline!
do you mean a recursive function? a recursive function is when you call a function within a function. for ex: function a() {a()} until a condition is met. That takes up more server resources then a loop would. You're best bet is a looping it. you can you while, for, etc..

July 4th, 2006
09:27 PM
Having a function run itself uses no more resources then a while loop. It is in many cases a practical decision. Like, in example listing directorys recursively.
___________________
I code alot.

July 5th, 2006
02:32 AM
Neverside Newbie
Status: Offline!
you can list directory in a loop. for example, I have a array(1,2,3,4,5)
i can use foreach($array as &$val) { echo $val; }
I can also use a recursive function
$a = array(1,2,3,4,5);
function b($val) {
$b = array_shift($val);
print $b;
if(sizeof($b) > 0) {
b($val);
}
}
b($a);
a recursive function is bulky and takes a little more resource to process than a loop function. And most languages have built in loop functions that can evaluate condition more effective than a recursive function. However, there are times when recursive function are the only way to solve the solution.