Re: [PHP] newbie question about preg_match

2004-05-20 Thread John W. Holmes
From: Al [EMAIL PROTECTED]

 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...

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



Re: [PHP] newbie question about preg_match

2004-05-20 Thread Al
If I have multiple instances that match the pattern, but only want the 
first one, which is the best way to handle it?

Putting the U flag seems to work, but I don't understand the full 
implications of using it here. 

preg_match(|$start(.*?)$end |Ui, $contents, $text);
Alternatively, I could use preg_match_all and use $text [1]. 

Thanks
John W. Holmes wrote:
From: Al [EMAIL PROTECTED]
 

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...
 

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


RE: [PHP] newbie question about preg_match

2004-05-20 Thread Chris W. Parker
Al mailto:[EMAIL PROTECTED]
on Thursday, May 20, 2004 12:51 PM said:

 If I have multiple instances that match the pattern, but only want the
 first one, which is the best way to handle it?

reread the last sentence in John's post.

 John W. Holmes wrote:
 
 From: Al [EMAIL PROTECTED]

[snip]

 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(). 



hth,
chris.

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