PHP Preg_Replace Help

Results 1 to 6 of 6
  1. #1
    Google my name... Komakech is offline
    MemberRank
    Nov 2011 Join Date
    EnglandLocation
    528Posts

    PHP Preg_Replace Help

    Hey guys.

    This is an issue with my regex and I can't understand why!

    Code:
    $format_search =  array(
          '#\[b\](.*?)\[/b\]#is',
          '#\[i\](.*?)\[/i\]#is',
          '#\[u\](.*?)\[/u\]#is',
          '#\[s\](.*?)\[/s\]#is',
          '#\[quote\](.*?)\[/quote\]#is',
          '#\[size=([1-9]|1[0-9]|20)\](.*?)\[/size\]#is',
          '#\[color=\#?([A-F0-9]{3}|[A-F0-9]{6})\](.*?)\[/color\]#is',
          '#\[url=((?:ftp|https?)://.*?)\](.*?)\[/url\]#i',
          '#\[url\]((?:ftp|https?)://.*?)\[/url\]#i',
          '#\[title=(.*?)\](.*?)\[/title\]#i'
       );
    $format_replace = array(
          '<strong>$1</strong>',
          '<em>$1</em>',
          '<span style="text-decoration: underline;">$1</span>',
          '<span style="text-decoration: line-through;">$1</span>',
          '<blockquote>$1</blockquote>',
          '<span style="font-size: $1px;">$2</span>',
          '<span style="color: #$1;">$2</span>',
          '<a href="$1">$2</a>',
          '<a href="$1">$1</a>',
          '<div class="box_header" id="$1"><center>$2</center></div>'
       );
    $str = preg_replace($format_search, $format_replace, $str);
       $str = nl2br($str);
       return $str;
    Every single BB code I have works perfectly, except the last one [title=blue]Title[/title]

    I don't see the problem, can anyone help?

    Thanks in advance!


  2. #2
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: PHP Preg_Replace Help

    The problem is simple. the regex for the ID of the title accepts all characters. You don't want that. The ']' character is one of many characters you don't want to accept.

    FYI, there are possible XSS injections up-and-down this thing..

  3. #3
    Google my name... Komakech is offline
    MemberRank
    Nov 2011 Join Date
    EnglandLocation
    528Posts

    Re: PHP Preg_Replace Help

    Quote Originally Posted by s-p-n View Post
    The problem is simple. the regex for the ID of the title accepts all characters. You don't want that. The ']' character is one of many characters you don't want to accept.

    FYI, there are possible XSS injections up-and-down this thing..
    I don't quite understand how to fix it though, would you mind giving me the code for the last one?

    '#\[title=(.*?)\](.*?)\[/title\]#i'

    As for the XSS injections, I only showed part of the function, I use htmlentities at the very beginning of the function to escape any XSS injections.

  4. #4
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: PHP Preg_Replace Help

    Quote Originally Posted by Komakech View Post
    I don't quite understand how to fix it though, would you mind giving me the code for the last one?

    '#\[title=(.*?)\](.*?)\[/title\]#i'

    As for the XSS injections, I only showed part of the function, I use htmlentities at the very beginning of the function to escape any XSS injections.
    PHP Code:
    '#\[title(=([a-z0-9-_\s]*))?\](.*?)\[/title\]#i'
    ...
    '<div class="box_header $2">$3</div>' 
    [title=blue]Title Goes Here[/title] (adds class 'blue' to the title)
    [title]Title Goes Here[/title] (uses no extra classes)
    [title=blue outline box]Title Goes Here[/title] (adds classes, blue, outline, and box)


    The reason I didn't use an ID is because IDs are pointless, restrictive and stupid. There is no reason to use IDs in web design unless you want a style attribute that can only be used once in the entire web-page- but that functionality doesn't even work since web designers use the ID incorrectly and browsers force multiple IDs to work, anyway. So never use IDs- they are broken.

    You can benefit from making this class-compliant, instead.

  5. #5
    Apprentice raptor34 is offline
    MemberRank
    Aug 2008 Join Date
    10Posts

    Re: PHP Preg_Replace Help

    Quote Originally Posted by s-p-n View Post
    The reason I didn't use an ID is because IDs are pointless, restrictive and stupid. There is no reason to use IDs in web design unless you want a style attribute that can only be used once in the entire web-page- but that functionality doesn't even work since web designers use the ID incorrectly and browsers force multiple IDs to work, anyway. So never use IDs- they are broken.

    You can benefit from making this class-compliant, instead.
    I'd disagree... If you're using Javascript, ID's are very useful to specify exactly what node to run the code on.

  6. #6
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: PHP Preg_Replace Help

    Quote Originally Posted by raptor34 View Post
    I'd disagree... If you're using Javascript, ID's are very useful to specify exactly what node to run the code on.
    No they aren't. Between all of the CSS selectors (tags, nth-child, classes), you can live without IDs much easier than you can live with them.

    What if an ID is used more than once? Since HTML and similar languages suppress errors and work with error-prone code, all IDs do is make development uncertain- which is a much worse disadvantage than any advantages there may be.
    Last edited by s-p-n; 11-04-12 at 09:35 PM.



Advertisement