Member Log In
Site Navigation
Latest Modifications
- [vB 3.8.4] THX - Hack for VB. 3.8.4
By: bluedog - [vB 3.8.4] Cyb - Chatbox V.2.3
By: bluedog - [vB 4.0.x] PHPKD - Advanced Quick...
By: PHPKD - [vB 3.6.x] StopSpam
By: flappi282 - [vB 3.8.x] vBulletin Chat Addon for...
By: 123flashchat
Latest Template Mods
- [vB 4.0.x] Remove My Profile Link...
By: Ak Worm - [vB 3.8.x] Images DownloadBox...
By: cRs!MP - [vB 3.8.x] Adviertise Mod On Forum...
By: MG4 - [vB 3.8.4] Images PassWordBox...
By: cRs!MP - [vB 3.8.4] Footer Follow Ups
By: Ak Worm
Latest Styles
- [vB 4.0.x] 4.0.3 - VB4STYLE-TWEETA...
By: Belon - [vB 4.0.x] [4.0.3] vbdesigns.de...
By: Belon - [vB 3.8.4] CompletevB - Skylight
By: DreadKnight - [vB 3.8.3] [vB 3.8.4] Barcelona...
By: hoiquantinhoc.com - [vB 3.8.3] Natures Walk by vBSkin...
By: Chri5
Latest Graphics
- [vB ] [anim.]Team Ranks
By: cRs!MP - [vB ] Abstract Circles (3...
By: cRs!MP - [vB ] PlayStation Rank Images
By: cRs!MP - [vB 3.6.12] Heavy Stroked Button...
By: Shelley - [vB ] Minature Ranks.
By: Shelley
vBulletin Modifications »
General vBulletin Section »
vBulletin Modification Tutorials »
[How-To] Validate fields or extract information using regular expressions
![]() |
| | #1 | |||||||||
Location: Israel Age: 29 ![]() |
Status: Offline [How-To] Validate fields or extract information using regular expressions 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: Regular Expression Examples Regular Expression Library 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" ( Alternate Last Post Display ) , 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. Regards, Idan. * Support will only be given via forums ! * If this post solved/aided your problem, please click "mark as aid" / "mark as solution" as explained in here Last edited by Idan; 06-06-2009 at 01:00 PM. | |||||||||
|
| | #2 |
| |
Status: Offline Thanks for the write-up it's all a bit greek to me, but I'm sure it'll be very helpful to others |
|
| | #3 | |||||||||
Location: Israel Age: 29 ![]() |
Status: Offline heh.. yea, regular expressions can be greek sometimes...:D You can imagine what it would look like if i would attempt to cover preg_match with perl-style patterns... ^_^ If anyone has some questions, dont be afraid to ask here - i can answer them here to better explain any complex matters that were mentioned in this article Regards, Idan. * Support will only be given via forums ! * If this post solved/aided your problem, please click "mark as aid" / "mark as solution" as explained in here | |||||||||
|
![]() |
« [How-To] Integrate Mod with forum search results
|
[How-to] Make Custom Profile Fields show in Postbit »
| Thread Tools | |
| |
All times are GMT. The time now is 05:21 AM.
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.























