Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

76 users online



Returning # of matches

Returning # of matches

Currently viewing this thread: 1 (0 members and 1 guests)


v3x0rg

v3x0rg

Pawn Coder
Status: Offline!

Returning # of matches

Here's the function I have:

PHP:

<?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:

<?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

BigToach

BigToach

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!

v3x0rg

v3x0rg

Pawn Coder
Status: Offline!

Could you give me an example, please?

---brandon

___________________

- Moderator at the official AMX Mod X forums

BigToach

BigToach

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!

BigToach

BigToach

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!

tekxtc

tekxtc

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..

erod

erod

uh yea
Status: Offline!

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.

tekxtc

tekxtc

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.

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0082 seconds.