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
https://forums.phpfreaks.com/topic/22572-how-would-i-go-about-doing-this/
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]

Archived

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

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