TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-05-2007, 11:28 PM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Asterix Variables in Regular Expession

I have a question of my own! I found out yesterday that you can add so-called variables in your regular expressions, although technically they're more like naming schemes for your array indexes. They can be done like so:

php Code:
$szText = 123;
preg_match('/(?P<first>\d{1})(?P<second>\d{1})(?P<third>\d{1})/', $szText, $aMatches);
print_r($aMatches);

Which would output:


Code:
Array
(
    [0] => 123
    [first] => 1
    [1] => 1
    [second] => 2
    [2] => 2
    [third] => 3
    [3] => 3
)
My question is, aside from what the correct term for them are, is this: is there any way to prevent the function from also adding the numeric indexes to the array and leave you with a pure associative array?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-06-2007, 02:13 AM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

They're called named capturing groups (as opposed to just capturing groups or non-capturing groups). There are a few alternative syntaxes in RegEx-land for doing this, but (?P<name>...) is all that PHP can use (PCRE).

More information is available on the ever useful regular-expressions.info site or in the PHP Manual. As far as I'm aware, the numeric key cannot be omitted from the resulting array of matches. That said it's a trivial matter to parse that array to remove items with a numeric key if you really wanted to.
Salathe is offline  
Reply With Quote
The Following 2 Users Say Thank You to Salathe For This Useful Post:
Matt83 (12-06-2007), Wildhoney (12-06-2007)
Old 12-06-2007, 02:57 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

An interesting read, Salathe. Thanks for pointing me in the right direction! Before last night I've only managed to read the first few pages of any regular expression documentation. There is some excellent stuff in php.net's manual for regular expressions, such as all the \S, \n.

I know it's not really much of an issue with the numeric items, but I was merely wondering if there was a flag you could set which prevented it. I'm guessing there isn't, though.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-06-2007, 03:27 PM   #4 (permalink)
The Contributor
Upcoming Programmer 
 
Matt83's Avatar
 
Join Date: Oct 2007
Location: Argentina
Posts: 72
Thanks: 18
Matt83 is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
More information is available on the ever useful regular-expressions.info site or in the PHP Manual.
Thanks for the links
__________________
http://www.mattvarone.com
Matt83 is offline  
Reply With Quote
Old 12-06-2007, 03:44 PM   #5 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

I guess I should read up on some RegEx too. I started a book on it once, got about 2-3 chapters in and never went back. It's not a subject that particularly interests me, but the power of RegEx cannot be ignored, it is a must for any serious programmer.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:36 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design