How can i Convert html tags to BBCode?

Discussion in 'PHP' started by baris22, Jan 10, 2009.

  1. #1
    hello,

    has anybody done this before? I want to convert all the html tags into bbcode like:

    
    <UL></UL>
    
    PHP:
    to

    [ list ][ /list ]

    (just take the space off before and after list. i could not write them together)

    is there any ready script to do this?
    thanks
     
    baris22, Jan 10, 2009 IP
  2. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Here you go! :)

    
    <?
    $your_text = "This is some <b>cool</b> <u>HTML-Tags</u>.. Make us BB-Tags";
    
    $htmltags = array(
                            '/\<b\>(.*?)\<\/b\>/is',                       
                            '/\<i\>(.*?)\<\/i\>/is',                           
                            '/\<u\>(.*?)\<\/u\>/is',
                            '/\<ul\>(.*?)\<\/ul\>/is'
                            );
    
    $bbtags = array(
                            '[b]$1[/b]',
                            '[i]$1[/i]',
                            '[u]$1[/u]',
                            '[list]$1[/list]'
                            );
    $your_text = preg_replace ($htmltags, $bbtags, $your_text);
    echo $your_text;
    ?>
    PHP:
     
    elias_sorensen, Jan 10, 2009 IP