Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    BBcode with Regular Expressions

    PHP Code:
    <?php
    function bbcode_html($input) {
    $bbcode = array(
    '/\[b\](.+?)\[\/b\]/i',
    '/\[i\](.+?)\[\/i\]/i',
    '/\[quote\](.+?)\[\/quote\]/i',
    '/\[url=(.+?)\](.+?)\[\/url\]/i'
    );

    $html = array(
    '<strong>$1</strong>',
    '<em>$1</em>',
    '<blockquote>$1</blockquote>',
    '<a href="$1">$2</a>'
    );

    return 
    preg_replace($bbcode$html$input);
    }
    ?>

  • #2
    New Coder
    Join Date
    Dec 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Although this code works when you write it on one line, it wont work if you do the following...

    [ quote ]
    hello
    world
    [ /quote ]

    because of the multiple lines.

    Does anyone know how to fix this?

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    The dot should also match newlines Youd have to add an "s" to the "i".
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #4
    New Coder
    Join Date
    Dec 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What if i wanted to nest quotes (which is usually standard in bbcode)?

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It should work as long as you don't nest the same bbcode (eg.
    ...
    Why not?
    ) which will fail but getting that right is kind of difficult.
    Last edited by marek_mar; 12-28-2005 at 06:06 PM.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •