Jump to content

How would i go about doing this?


GuitarGod

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  ;)
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.