note 58141 added to function.preg-match-all

From: Date: Tue, 25 Oct 2005 15:43:33 +0000
Subject: note 58141 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-97578@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58141 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58141&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58141&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58141&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58141&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58141&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58141&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58141&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58141&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97578) next »