Simple Regular Expression

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Simple Regular Expression

Post by pickle »

Hi All,

Regular expressions are weird and scary to me, but this seems pretty simple.

I'm trying to chunk through an LDAP domain name. An example string would be:

Code: Select all

cn=ART999,ou=testobject,o=University
Now, what I'm trying to do is determine if the above string has a course in it. The course will always be of the form XXXYYY where X = an alphabetic character and Y = an integer. Ideally I'd like only the course name returned, but if it doesn't happen, oh well.

The regular expression I've built so far goes like this:

Code: Select all

$name = split("cn=їa-zA-Z]{3}ї0-9]{3}",$passed_object_name);
Then, I do a test on $name and if it's populated, I found a hit.

For some reason, $name[1] will get everything after the match, but $name[0] is empty. So from what I can figure, split() is finding the match, just not putting it in $name correctly. Any thoughts? Thanks.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

split is intended for splitting =) So pattern describes the 'element delimiter' by which the source string is splitted then. Use [php_man]eregi[/php_man] to capture substring like yours.

Code: Select all

if(eregi("(cn=їa-zA-Z]{3}ї0-9]{3})",$passed_object_name,$matches)) {
   echo $matchesї1]; // prints 'cn=XXXYYY'
 }
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Awesome, thanks. Since you seem to be somewhat versed in regular expressions - could you tell me if there's a simple way to just have XXXYYY returned, or do I have to break apart the string afterwards? Thanks again.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply