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

Posted by siromega on 05/31/06 17:54

Rik wrote:
>
>
> I hate to be the bearer of bad tidings, but it will fail on single escaped
> \":
>
> $str = '"baz,\"bax", bay, foz, "a, b", "foo';
> $expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
> $results=preg_split($expr,trim($str));
> $res = preg_replace("/^\"(.*)\"$/","$1",$results);
> print_r($res);
>
> Array
> (
> [0] => "baz
> [1] => \"bax"
> [2] => bay
> [3] => foz
> [4] => "a, b"
> [5] => foo
> )
>
> Properly nested, it can handle it:
> $str = '"baz,\"bax\"", bay, foz, "a, b", foo';
>
> Array
> (
> [0] => baz,\"bax\"
> [1] => bay
> [2] => foz
> [3] => "a, b"
> [4] => foo
> )
>
> Also a nice one is:
> $str = 'bay, foz, "a, b", "fo\"o"';
>
>
> Array
> (
> [0] => bay, foz, "a
> [1] => b", "fo\"o"
> )
>
>
> Not a very robust one here.
> But maybe it's OK for your data, it all depends how much you know for sure
> about that.
>
> Grtz,
> --
> Rik Wasmus

Indeed, that is correct. I'm going to have to chcek out my data and see
if it'll work. Do you happen to know if fgetcsv() properly handle the
single " ?

 

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

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