RE: [PHP] how can i lookup the index in a string where a regexp matches?
| From: | Boyd, Todd M. | Date: | Tue, 18 Nov 2008 14:54:04 +0000 |
| Subject: | RE: [PHP] how can i lookup the index in a string where a regexp matches? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-283454@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Rene Veerman [mailto:rene7705@gmail.com]
> Sent: Tuesday, November 18, 2008 8:50 AM
> To: php php
> Subject: Re: [PHP] how can i lookup the index in a string where a
> regexp matches?
>
> Rene Veerman wrote:
> > hi, i need to search with regexps in strings, and want to know the
> > index (of the source string) at which the regexp matched. and the
> > length of the matched string, so with substitutions like \d+..
> >
> > i'm kinda in a hurry on this one, would appreciate your immediate
> > answer very much..
> >
> :D i shouldnt scan texts to fast anymore.. esp when i'm in a hurry
>
> http://nl.php.net/manual/en/function.preg-match.php
>
> |<?php
> $subject = "abcdef";
> $pattern = '/^def/';
> preg_match($pattern, substr($subject,3), $matches,
> PREG_OFFSET_CAPTURE);
> print_r($matches);
> ?> |
>
> will produce
>
> Array
> (
> [0] => Array
> (
> [0] => def
> [1] => 0
> )
>
> )
Are you sure about the ^? I would think '/^def/' would look for "def" at
the beginning of the line... anyway, glad to see you found out about the
PREG_OFFSET_CAPTURE flag.
// Todd