Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

BBCode conversion

Last post 08-24-2008, 1:31 PM by pandor. 8 replies.
Sort Posts: Previous Next
  •  08-23-2008, 8:30 PM 45579

    BBCode conversion

    Hi, I asking for help in my PHP code optimization. I try to rewrite my old/slow BBCode conversion function to use regex. The allowed BBCodes are

    [ b]text[ /b]
    [ i]text[ /i]
    [ url]domain.com[ /url] or [ url]domain.com|domainname[ /url]
    [ img]filename.ext[ /img] or [ img]filename.ext|description[ /img]

    It is possible to construct regex with the following conditions?

    [ b] - inside only [ i] and [ url] BBCode allowed, newline NOT allowed
    [ i] - as [ b]
    [ url] - inside NO BBCode allowed, newline NOT allowed
    [ img] - as [ url]

    (I try to make difference between two [ url] (or [ img]) syntax with '/\[url\](.+?)\[\/url\]/is' and '/\[url\](.+?)\|(.+?)\[\/url\]/is' patterns, but it generates malfunction.)

    Thanks for help in advance 

  •  08-23-2008, 10:09 PM 45580 in reply to 45579

    Re: BBCode conversion

    Are you testing a string to see if it matches the conditions outlined?

    If so, if the string matches the following regex it has failed at least 1 of your conditions:

    if(preg_match('#(?=.*(?:\[([bi])\](?:(?!\[/\1\]).)*?(?:\[(?:img|b)\]|\n))|(?:\[(url|img)\](?:(?!\[/\2\]).)*?(?:\[[a-z]+\]|\n)))#s',$sourcestring)){

      echo "failed validation";

    }

    If you are not trying to validate based on the conditions given, possibly explain the question further.


  •  08-24-2008, 7:06 AM 45582 in reply to 45580

    Re: BBCode conversion

    I don't validate the string, just replace tags one-by-one with preg_replace (if matches for the pattern)
    for example
    preg_replace('/\[b\](.+?)\[\/b\]/is', '<b>$1</b>', $str);
    preg_replace('/\[img\](.+?)\[\/img\]/is', '<div style='text-align:center'><img src='$1'></div>', $str);

    I guess the (complex) pattern above matches for every conditions I mentioned
    To understand the formula, please tell me what will be the pattern only for [ b] (for example)?

    Thanks

  •  08-24-2008, 8:11 AM 45583 in reply to 45582

    Re: BBCode conversion

    if(preg_match('#(?=.*(?:\[([bi])\](?:(?!\[/\1\]).)*?(?:\[(?:img|b)\]|\n))|(?:\[(url|img)\](?:(?!\[/\2\]).)*?(?:\[[a-z]+\]|\n)))#s',$bbcode)){
      echo 'failed validation';
    } else {
      $bbcode=preg_replace('#\[(/?[bi])\]#i','<$1>',$bbcode);
      $bbcode=preg_replace('#\[img\](.*?)\[/img\]#i',"<div style='text-align:center'><img src='$1'></div>',$bbcode);
      $bbcode=preg_replace('#\[url\](.*?)\[/url\]#i',"<a href='$1'>$1</a>",$bbcode);
      echo $bbcode;
    }


  •  08-24-2008, 9:07 AM 45584 in reply to 45583

    Re: BBCode conversion

    Thank you for the fast response.
    What is the regex pattern for [ url]domain.com|webpage[ /url] to <a href='domain.com'>webpage</a>
    while keep working the '#\[url\](.*?)\[/url\]#i' pattern?
    (For example in case [ url]domain1.com[ /url] [ url]domain2.com|webpage2[ /url] [ url]domain3.com[ /url])

    And how to modify the
    $bbcode=preg_replace('#\[url\](.*?)\[/url\]#i',"<a href='$1'>$1</a>",$bbcode);
    patternt to match just in case if no [ b] , [/b] , [  i] , [ /i] , [ img] , [ /img] and newline tags inside [ url] and [ /url]

  •  08-24-2008, 12:12 PM 45585 in reply to 45584

    Re: BBCode conversion

    Let's say that you remove the validation logic and just apply the preg_replace function to the string in steps.

    Let's start with [b]:  for [b] tags which do not have [b] or [img] tags within their enclosing [/b] block:

    $bbcode=preg_replace('#\[b\]((?:(?!\[/?(?:img|b)\]|\n).)*?)\[/b\]#i','<b>$1</b>',$bbcode);

    If that preg_replace function works for you as desired that should give you the framework for [i] and [img].

    For [url]:

    <?php
    $bbcode='[url]http://test.com[/url]
    [url]http://test2.com|test2[/url]';
    $bbcode=preg_replace('#\[url\]((?:(?!\[(?:b|img|i)\]).)*?)\|(.*)\[/url\]#i',"<a href='$1'>$2</a>",$bbcode);
    $bbcode=preg_replace('#\[url\]((?:(?!\[(?:b|img|i)\]).)*?)\[/url\]#i',"<a href='$1'>$1</a>",$bbcode);
    echo '<pre>'.$bbcode;
    ?>


  •  08-24-2008, 12:47 PM 45586 in reply to 45585

    Re: BBCode conversion

    Thanks. It works fine when url tags are separated with newline, but without newline it generates strange output.
    Try $bbcode='[ url]http://test.com[ /url] [ url]http://test2.com|test2[ /url]';

  •  08-24-2008, 1:14 PM 45587 in reply to 45586

    Re: BBCode conversion

    Try:

    <?php
    $bbcode='[url]http://test.com[/url]test[url]http://test2.com|test2[/url]test[url]http://test.com[/url]test[url]http://test2.com|test2[/url]';
    $bbcode=preg_replace('#\[url\]((?:(?!/?\[(?:url|b|img|i)\]).)*?)\|(.*?)\[/url\]#i',"<a href='$1'>$2</a>",$bbcode);
    $bbcode=preg_replace('#\[url\]((?:(?!/?\[(?:url|b|img|i)\]).)*?)\[/url\]#i',"<a href='$1'>$1</a>",$bbcode);
    echo '<pre>'.$bbcode;
    ?>


  •  08-24-2008, 1:31 PM 45588 in reply to 45587

    Re: BBCode conversion

    perfect. thanks again.
View as RSS news feed in XML