preg_match and delimited strings
- From: siromega@xxxxxxxxx
- Date: 30 May 2006 11:58:48 -0700
I have a string I'd like to have broken into parts using preg_match. I
used a regular expression from the Perl FAQ (http://perlfaq.cpan.org/):
push(@new, $+) while $text =~ m{
"([^\"\\]*(?:\\.[^\"\\]*)*)",? # groups the phrase inside the
quotes
| ([^,]+),?
| ,
}gx;
The idea is that it splits delimited strings respecting the quotes, for
example...
foo, bar, "foo, bar", bar
would end up as
-foo
-bar
-"foo, bar"
-bar
So obviously an explode wont work.
I cant figure out how to convert that piece of perl above into
preg_match. I've copied the string and escaped all the appropriate
charecters however it still wont divide the string show above
properly...
$str = "foo, bar, \"foo, bar\", bar";
$re = "\"([^\\\"\\\\]*(?:\\\\.[^\\\"\\\\]*)*)\",?| ([^,]+),?| ,";
if (preg_match($re, $str, $res)) {
print_r($res);
}
I have other strings that need parsing too (including ones with a
double encapsulator inside the string - eg. 'don''t touch that!'). But
I'm saving those for later.
.
- Follow-Ups:
- Re: preg_match and delimited strings
- From: Rik
- Re: preg_match and delimited strings
- Prev by Date: Re: php mail() issues
- Next by Date: Re: animated gif
- Previous by thread: php mail() issues
- Next by thread: Re: preg_match and delimited strings
- Index(es):
Relevant Pages
|