[PHP] Re: regular expression to extract from the middle of a string

2006-07-14 Thread Al

Steve Turnbull wrote:

Hey folks

I don't want to just get you to do the work, but I have so far tried
in vain to achieve something...

I have a string similar to the following;

cn=emailadmin,ou=services,dc=domain,dc=net

I want to extract whatever falls between the 'cn=' and the following
comma - in this case 'emailadmin'.

Question(s);
is this possible via a regular expression?
does php have a better way of doing this?

Some pointers would be greatly appreciated. Once I have working, I will
be creating a function which will cater for this and will post to this
list if anyone is interested?

Cheers
Steve



$pattern= %cn=([a-z]+)%i;   //Or, if numbers are ok, you an use \w 
instead of [a-z]

preg_match($pattern, $your_string, $match);

$value= $match[1];

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: regular expression to extract from the middle of a string

2006-07-14 Thread Adam Zey

Steve Turnbull wrote:

Hey folks

I don't want to just get you to do the work, but I have so far tried
in vain to achieve something...

I have a string similar to the following;

cn=emailadmin,ou=services,dc=domain,dc=net

I want to extract whatever falls between the 'cn=' and the following
comma - in this case 'emailadmin'.

Question(s);
is this possible via a regular expression?
does php have a better way of doing this?

Some pointers would be greatly appreciated. Once I have working, I will
be creating a function which will cater for this and will post to this
list if anyone is interested?

Cheers
Steve



The fastest way to do this probably does not involve explodes or regular 
expressions. I would wager that the fastest method would be to do a 
strpos to find cn=, then a strpos to find the next comma (strpos 
supports offsets to start looking for), and then substr out the segment 
you've identified.


However, if cn= is ALWAYS first, it's actually a lot simpler. Then it 
becomes something like this:


$mystring = cn=emailadmin,ou=services,dc=domain,dc=net;
$cnpart = substr($mystring, 2, strpos($mystring, ,) - 3 );

My offsets might be a bit off there, I always get confused with them and 
have to verify by running the code in case I mixed up a zero-based 
character position or something :P


Regards, Adam.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php