Jump to content


Photo

How would i go about doing this?


  • Please log in to reply
3 replies to this topic

#1 GuitarGod

GuitarGod
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 30 September 2006 - 07:59 AM

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:

[quote]blahblah[/quote]

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  ;)

#2 intrik

intrik
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 30 September 2006 - 08:02 AM

Try using
http://au.php.net/str_replace

#3 GuitarGod

GuitarGod
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 30 September 2006 - 08:13 AM

yeah, but i was also thinking along the lines of:

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

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

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 30 September 2006 - 09:17 AM

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users