Jump to content

Archived

This topic is now archived and is closed to further replies.

GuitarGod

How would i go about doing this?

Recommended Posts

Hello :D

My question is very similar to a BBcode thing. It's kinda hard to explain so i'll do my best ;). Lets say for example i have a string containing:

[code][quote]blahblah[/quote][/code]

How would i use PHP to look for the [ quote] and [/ quote], more importantly how can i also get the value between these tags (in this case the 'blahblah'). I suck at explaining PHP problems, so sorry if you don't understand  ;)

Share this post


Link to post
Share on other sites
yeah, but i was also thinking along the lines of:

[code][quote="Username"]TEXT[/quote][/code]

I'm not sure whether str_replace could do this, could it?

Share this post


Link to post
Share on other sites
try
[code]
<?php

function extractText ($str, $bb='', $startpos=0) {
    $find = '[' . $bb;
    $p1 = strpos($str,$find,$startpos);      // start of open tag
   
    if ($p1===false) return $str;
   
    $p2 = strpos($str, ']', $p1);            // end of open tag
   
    $find = '[/' . $bb;
    $p3 = strpos($str, $find, $p2);          // start of close tag
   
    $res = $p3 ? substr($str, $p2+1, $p3-$p2-1) : substr($str, $p2+1);
   
    if (!$bb)
        return extractText($res);
    else
        return $res;
   
}


$txt = '[quote="Username"][B]TEXT[/B][/quote]';

echo extractText($txt, 'quote');    //-->  [B]TEXT[/B]
echo extractText($txt, 'B');        //-->  TEXT
echo extractText($txt);            //-->  TEXT
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.