Jump to content

regular expression


birbal

Recommended Posts

objective1) trying to make bbcode using preg_replace2) one tag will have two atributes color and size. both are optional eg ,[b color=red size=3]3) size will have value 1-9, color have only value red green blue yellow black white.4) will match when: [b color=OPTIONS],[b color=OPTIONS size=1-9]5) wont match when: [b, [b color=,[b color,[bcolor,.........so on[u]i have figured out a regexp[/u]\[b(?: (color)=(white|red|blue|green|yellow|black))?(?: (size)=([0-9]))?\]resultit is showing ok at validator.need some confirmation and correction. thank you :)

Link to comment
Share on other sites

I'm sort of surprised it works, because AFAIK, "(?:" means "not followed by [whatever is up to the ")"]", so in theory, this shouldn't match what you want to match, but will match

[b]

(plain opening)

[b size=0 color=white]

(reverse valid values)

[b size=13 color=aqua]

(reverse invalid values)

[b color=aqua size=13]

(normally ordered invalid values)I'd reccomend using PEAR's HTML_BBCodeParser, or NBBC, or, if even those don't suit you, write your own BBCode parser as you're trying to do, but make sure to create automated PHPUnit tests (for you; not for your users).A complex matter like BBCode must not be taken lightly.

Link to comment
Share on other sites

I'm sort of surprised it works, because AFAIK, "(?:" means "not followed by [whatever is up to the ")"]", so in theory, this shouldn't match what you want to match, but will match
In this case (?: means don't capture the subpattern.The pattern looks good to me. If present, it will capture the word color, the color specified, the word size, and the size specified.
Link to comment
Share on other sites

I'm sort of surprised it works, because AFAIK, "(?:" means "not followed by [whatever is up to the ")"]", so in theory, this shouldn't match what you want to match, but will match
[b]

I think you're thinking of '(?!'Mike Campbell is correct. (?: is a non-capturing subpattern.Far as I can tell, that regex should work just fine.
Link to comment
Share on other sites

thanks to all shadowmage mike robot for confirming. btw it is valdating all but is not validating reverse order (valid option)eg. [b size=9 color=red]what i am missing?

Link to comment
Share on other sites

I'd reccomend using PEAR's HTML_BBCodeParser, or NBBC, or, if even those don't suit you, write your own BBCode parser as you're trying to do, but make sure to create automated PHPUnit tests (for you; not for your users).A complex matter like BBCode must not be taken lightly.
what is phpunit test? what does it do and how? i am not getting that..i saw thir site but not getting all how to use that ..as i did not use that before. plese give me a little start up here.
Link to comment
Share on other sites

PHPUnit is a framework for creating and running automated tests.Basically, you create special functions in which you run your code with some input, and declare the output you're expecting. Upon running the test suite, the framework checks if the actual output matches the expected one, and shows an error in a certain fashion (usually, the command line) if that's not the case. When you make changes to your code, you can run your test suite, and see if the new code breaks anything, and fix it immediatly (or, as I happen to often do, simply delay shipping of the new version and fix it later) if it does. The more exsauistive your tests are, the higher quality of your application will be.Read this article for a more detailed introduction.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...