1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Troublesome function...

Discussion in 'PHP' started by PoPSiCLe, Mar 16, 2009.

  1. #1
    I have a function I use for str_replace some BB-codes and such:
    
    function bbcode_to_html()
    {
    global $tekst;
    $tekst = str_replace("[B]","<strong>",$tekst);
    $tekst = str_replace("[/B]","</strong>",$tekst);
    $tekst = str_replace("[I]","<em>",$tekst);
    $tekst = str_replace("[/I]","</em>",$tekst);
    $tekst = str_replace("[U]","<span class=\"underline\">",$tekst);
    $tekst = str_replace("[/U]","</span>",$tekst);
    $tekst = str_replace("[b]","<strong>",$tekst);
    $tekst = str_replace("[/b]","</strong>",$tekst);
    $tekst = str_replace("[i]","<em>",$tekst);
    $tekst = str_replace("[/i]","</em>",$tekst);
    $tekst = str_replace("[u]","<span class=\"underline\">",$tekst);
    $tekst = str_replace("[/u]","</span>",$tekst);
    $tekst = str_replace("[!]","<span class=\"important\">",$tekst);
    $tekst = str_replace("[/!]","</span>",$tekst);
    $tekst = str_replace("\r\n","<br />",$tekst);
    $tekst = str_replace("&","&amp;",$tekst);
    $tekst = preg_replace('/\[url\=(.*?)\](.*?)\[\/url\]/is','<a href="\\1">\\2</a>',$tekst);
    }
    
    PHP:
    It works, but it's sort of... limited in the way it can be used.

    It involves me having the $tekst-variable present in the page, of course, and therefore it is sort of not useful if I have more than one variable I need to parse through the function.

    The function itself is stored in a global_variables.php-file.

    What I would like to be able to do, with this function, is to parse a variable to it, from the page, and then return that variable to the page using the function - although I'm a little unsure as to how to do this, and also, how to integrate this function into other functions in different files.

    Anyone have any tips, links etc?
     
    PoPSiCLe, Mar 16, 2009 IP
  2. renlok

    renlok Well-Known Member

    Messages:
    457
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #2
    You chould change it to
    
    function bbcode_to_html($tekst)
    {
    $tekst = str_replace("[b]","<strong>",$tekst);
    $tekst = str_replace("[/b]","</strong>",$tekst);
    $tekst = str_replace("[i]","<em>",$tekst);
    $tekst = str_replace("[/i]","</em>",$tekst);
    $tekst = str_replace("[u]","<span class=\"underline\">",$tekst);
    $tekst = str_replace("[/u]","</span>",$tekst);
    $tekst = str_replace("[b]","<strong>",$tekst);
    $tekst = str_replace("[/b]","</strong>",$tekst);
    $tekst = str_replace("[i]","<em>",$tekst);
    $tekst = str_replace("[/i]","</em>",$tekst);
    $tekst = str_replace("[u]","<span class=\"underline\">",$tekst);
    $tekst = str_replace("[/u]","</span>",$tekst);
    $tekst = str_replace("[!]","<span class=\"important\">",$tekst);
    $tekst = str_replace("[/!]","</span>",$tekst);
    $tekst = str_replace("\r\n","<br />",$tekst);
    $tekst = str_replace("&","&amp;",$tekst);
    $tekst = preg_replace('/\[url\=(.*?)\](.*?)\[\/url\]/is','<a href="\\1">\\2</a>',$tekst);
    return $tekst;
    }
    
    PHP:
    so to use it you would just have to put something like
    
    $something_else = bbcode_to_html($something);
    
    PHP:
     
    renlok, Mar 16, 2009 IP
    PoPSiCLe likes this.
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    And that, for some reason, does not seem to work... NEVERMIND

    It works just fine - I'm just being extremely stupid sometimes :)

    (Left a variable that f*cked up everything, not at all weird I couldn't get it to work).

    Thanks again!
     
    PoPSiCLe, Mar 16, 2009 IP