• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

BB Code Option Validation

we_are_borg

Active member
#1
Can you give an example based on this code

Code:
<span style="background-color: {option}; padding: 0 2px">{text}</span>

Usage: [bg=red]This is some text with a red background[/bg]
 

Chris D

XenForo developer
Staff member
#3
This is the "Option Match Regular Expression" from the GALLERY BB code from XFMG:
Code:
/^media,(|\s)(\d+)$/i
^^^ Brogan's example is also good :)

In terms of escaping the option, you may have already seen that in the other code example I gave:
PHP:
$type = $formatter->filterString(array_shift($parts),
   array_merge($rendererStates, array(
      'stopSmilies' => true,
      'stopLineBreakConversion' => true
   ))
);
For any further support with this, it would be most appropriate to create a new thread in the XenForo Development Discussions forum.
 

we_are_borg

Active member
#5
Thank you moderator for splitting this topic from the bug report.

The code that brogan gave worked like a charm no lets see if i can read it correctly am i novice on regular expressions.

Code:
/^(rgb\(\s*\d+5?\s*,\s*\d+%?\s*,\s*\d%?\s*\)|#[a-f0-9]{3}|#[a-z]+)$/i
Code:
(rgb\(\s*\d+5?\s*,\s*\d+%?\s*,\s*\d%?\s*\)
What i read here if someone uses rgb to define the color that he is only allowed to use numbers and two coma's. But how do you limit the to max 3 numbers in the sequence, that not clear to me?

The | i assume is the next part of the regular expression

Code:
#[a-f0-9]{3}
If some one put the color up in hex then its limited to numbers and a, b, c, d, e and f. The question here is how the expression knows that 1234567 is not a valid sequence and what is the {3} for

Code:
#[a-z]+)$/i
The next one is for named colors you say only letters may be used. But what is the $/i for?

Thank you for learning me new stuff i did not know you needed to use it to make BB Code save.
 

Chris D

XenForo developer
Staff member
#6
FWIW I think you may have copied the regex from the screenshot incorrectly.
Code:
^(rgb\(\s*\d+%?\s*,\s*\d+%?\s*,\s*\d+%?\s*\)|#[a-f0-9]{6}|#[a-f0-9]{3}|[a-z]+)$
In the rgb case it ensures that 3 numerical values can be entered, separated by commas.

If some one put the color up in hex then its limited to numbers and a, b, c, d, e and f. The question here is how the expression knows that 1234567 is not a valid sequence and what is the {3} for
It matches a string that is exactly 3 characters long, and the characters must be one of 0-9 or a-f. 1234567 is not valid because it is more than 3 characters.

There's another bit you missed before that which is similar, but it matches a string which is 6 characters long.

The next one is for named colors you say only letters may be used. But what is the $/i for?
The start of the regex has a ^, this asserts the match must begin at the start of the string. $ is the opposite. It asserts the match must now be at the end of the string.

Typical example:

"#ff0000" will match but
"#ff0000 blah" will not match

The / is the delimiter - there's one at the beginning, too. It's the start and end of the pattern. The i is a modifer. There are others, but i basically allows the match to be case insensitive. In short, this would match, too, despite the pattern seemingly only allowing a-f (lower case):

"#FFFFFF"