The Arcive of vBulletin Modifications Site. |
|
Details »»
|
|||||||||||||||||||||||||
When you're using the ereg() expression, and you have multiple items inside it, such as this:
PHP Code:
Show Your Support
|
|||||||||||||||||||||||||
| Comments |
|
#2
|
||||
|
||||
|
That's because you had something like this:
Code:
[0-9]{1}?
By putting ? outside the parentheses, you are saying you want the whole thing (a digit that appears exactly once) to appear 0 or 1 times, which is better (but still pointless ).There is no need in putting {1} because that has no affect on the outcome of the regex. Also you should use preg_match(), it's faster.
|
|
#3
|
||||
|
||||
|
So I could either use ([0-9])? or ([0-9]{0,1}) and either is the exact same thing, correct?
Also, as to the preg_match, do I just replace all instances of "ereg" with it, or do I need to recode anything (in other words, is it the exact same syntax)? |
|
#4
|
||||
|
||||
|
No, not exactly the same. You need to add delimiters before and after the expression. Most use / as the delimiter but I prefer # as it's less likely to appear in the expression itself (because if / is in it, you must escape it like \/).
The parentheses matter, don't get me wrong. The matches you will have in the $regs array will only contain what's between them. So if you put something out of the parentheses, you won't get it in the matching array. |
|
#5
|
||||
|
||||
|
So what would be the best way to write an expression that looks for either a single digit or no digit?
Looking at my original expression, PHP Code:
M1/1SM M1SM M11SM 1/1SM 1SM 11SM (where '1' represents any single-digit number, all other characters are literal) Any of those substrings will be surrounded by a blank space on both sides. SM will always appear in it, and will always be preceded by at least one digit, possibly two. If it's preceded by one digit, that one digit may or may not be preceded by an M. If that one digit is not preceded by an M, it may or may not be preceded by a forward slash; if it is, it must be preceded by another single-digit number, and that single digit may or may not be preceded by an M. If the SM is preceded by two digits, those two digits may be preceded by an M, and that's it. And how I need all that grouped into something I can work with (by putting it into $regs[x]) is that each element should be separate, unless it's two digits together, because that is one number. Also, if there is a forward slash, the single digit before it and the single digit after it, and the slash, can all go together, although I think it would be easier to smash them together later (that is a fraction). |
![]() |
|
|