Re: newbie question about preg_match
| From: | John W. Holmes | Date: | Thu, 20 May 2004 16:52:46 +0000 |
| Subject: | Re: newbie question about preg_match | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-186606@lists.php.net to get a copy of this message | ||
From: "Al" <news@ridersite.org>
> I'm trying to compose a general purpose text snip expression to extract
> a text segment from a string.
>
> Should this work for all reasonable cases? It seems to work for several
> test strings.
>
> $start= "str1";
>
> $end= "str2";
>
> preg_match ("|$start (.*) ? $end |i", $contents, $text);
>
> $segment= $text[1];
Looks like you have some extra spaces in there that'll mess it up. The '?'
for example, is only applying to the space before it.
You want
preg_match("|$start(.*?)$end |i", $contents, $text);
Note that this will only return one match when there could be more,
depending upon your string. If you want all of them, use preg_match_all().
---John Holmes...