The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
How-To Validate fields or extract information using regular expressions
This guide/tutorial was made to answer few of questions asked by Come2Daddy. This guide will give the trade tools for validating fields when processing any HTML forms (before saving information to DB). Or in general will provide information on how to "analyze" certain text block, to "extract" the information we need. so, lets begin Intro: Regular Expression, also known as "regex" or "regexp" in short, is combination of special chars & letters that describes a "search pattern". The regular expression is a big area (entire books could be written about it). In this tutorial i'll give focus to php functions, since guide goal is to give you the tools for using it inside vbulletin plugins. php has several functions that allow regular expression handling. We'll cover the search functions commonly used: preg_match PHP Code:
ereg/eregi PHP Code:
ereg is "Case Sensitive" & eregi is "Case Insensitive". Note these functions are much easier to handle (than the preg_match), and this guide will focus mainly on it, since the perl-style regexp pattern is more complex to understand. Before we can gets hands on, we needs to explain how to create a regular expression pattern(s). Note: i wont be listing all of them - only the most common ones. as said previously, this regexp is big field and i dont want too many bushes hiding what i wanted people to learn, if you know what i mean... Text Patterns:
Common & useful patterns:
Note: the list of regex possibilites are endless, and almost each field or pattern needs close exmaining of it, to avoid any "pitfalls". Some sites offer listing of such common patterns. Will provide refrence to 2: http://www.regular-expressions.info/examples.html http://regexlib.com/DisplayPatterns.aspx php & vbulletin usage examples: As mentioned in the begining of this guide, i will mainly focus on ereg() & eregi() functions, as they are quite easy to use. Fetch field based on pattern (and manipulate data in plugin code): Lets assume we have following string: "Welcome back Idan" (where Idan is my name, and "Welcome back" is constant string text) and suppose i would want to extract my name from above string, i could use the following pattern: "^Welcome back ([a-zA-Z]+)$". So in code this would look like this: PHP Code:
In vbulletin we could find various examples of usages that can help us, when we want to use regular expressions. Lets assume we want to extract certain informaion from given template, or even anaylze data from it. I'm reffering to another mod i've coded that uses regular expression - mod called "Alternate Last Post" (https://vborg.vbsupport.ru/showthread.php?t=214832) , in that mod concept, i've used regular expressions pattern search to match for date fields, so i can calculate specific time from it. In that mod i search pattern the $lastpostinfo[lastpostdate] and $lastpostinfo[lastposttime] to break it down to parts, in way that would allow to obtain day, month, year, hour, minutes, seconds, etc. & with it allow any code manipulation later on. Require Field Validation (or redirect with error example): if we want to redirect with error messge based on regexp pattern of the field we can do following code into plugin hook: PHP Code:
Be sure to add first the error phrase under "phrase manager" on admincp. One more common "solution" regular expressions allow us to do is "remote data fetching" - open remote page, and assuming the pattern is fixed & known to us, extract information we need from it using pattern search. Non-English Character Matching: The "character set" block (square brackets) allow us to match characters, but since we can only range english chars, one trick is to use ASCII or UNICODE matching like this: ASCII matching can be performed like this: ([^\x00-\x80]+) Unicode matching can be performed like this: [^\u0000-\u0080]+ To our case, to match only non-english chars use: ([^\x00-\x80]+) To match ALL chars (both english & non english & some non-chars as well, perhaps) use: ([a-zA-Z\x00-\xFF]+) That's it for this tutorial. I really hope this tutorial helped to build basic regexp knowledge with some refrence to go with, that one can use in his modifications coding. |
#2
|
|||
|
|||
first of all, I'd love to let you know how greatful I'm for this tutorial
thank you very much, I read it and I couldn't think of a question I just liked it, and enjoied it as I like your style of writing which reflects how a great reader you are thanks again, and my vote is Excellent, indeed |
#3
|
|||
|
|||
IdanB
Thank you for the information. I have 2 issues I'm trying to solve. 1st - how can I make the referral code mandatory? I know you can enable the feature, however the the person registering can leave it blank and still register. I need all people registering to use a current members userid and if they don't a error page will display with a email address to get one. 2nd - Do you know if there is a way to copy "Child folders" and their subfolders and to paste them in other areas of the forum. I am creating a forum of all the states in the US. I have one state setup and it would be "REAL" nice to copy and paste into olther states. Thank You Ray |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|