You are here: preg_match and delimited strings « PHP Programming Language « IT news, forums, messages
preg_match and delimited strings

Posted by siromega on 05/30/06 18:58

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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация