note 58141 deleted from function.preg-match-all by nlopess
| From: | nlopess@php.net | Date: | Wed, 07 Dec 2005 22:11:16 +0000 |
| Subject: | note 58141 deleted from function.preg-match-all by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-99715@lists.php.net to get a copy of this message | ||
Note Submitter: sharky at vitostudio dot ru
----
Split comma separated strings into key-value. Values can be in quotes or apostrofes.
Quotes inside quotes may have slash before it, ex. a="blabla \"citation\"".
May be usefull for parsing SQL queries, HTML/XML tag parameters etc.
If You want to change delimiter just change comma at the end of regex "(?:,|\z)" to any
character you want.
For using with multiline in PHP pregs don't forget "s" flag with "m".
<?php
if (preg_match_all('/(\w+)\s*=\s*((?:\')(.*?)(?:(?<!\\\))'.
'(?:\')|(?:\")(.*?)(?:(?<!\\\))(?:\")|(.*?))(?:,|\z)/smi', $str, $tmp))
{
foreach ($tmp[0] as $key=>$val) {
$result['fields'][] = $tmp[1][$key];
$result['values'][] = $tmp[2][$key];
}
} else $result = array('fields'=>false, 'values'=>false);
?>
Maybe it help anybody. I use this in my SQLParser class for separate fields/values in UPDATE query.
Best regards, Sharky.
http://sharky.vitos.ru