note 92632 added to function.preg-split

From: Date: Sat, 01 Aug 2009 07:57:24 +0000
Subject: note 92632 added to function.preg-split
Groups: php.notes 
Request: Send a blank email to php-notes+get-158983@lists.php.net to get a copy of this message
Extending m.timmermans's solution, you can use the following code as a search expression parser: <?php $search_expression = "apple bear \"Tom Cruise\" or 'Mickey Mouse' another word"; $words = preg_split("/[\s,]*\\\"([^\\\"]+)\\\"[\s,]*|" . "[\s,]*'([^']+)'[\s,]*|" . "[\s,]+/", $search_expression, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); print_r($words); ?> The result will be: Array ( [0] => apple [1] => bear [2] => Tom Cruise [3] => or [4] => Mickey Mouse [5] => another [6] => word ) 1. Accepted delimiters: white spaces (space, tab, new line etc.) and commas. 2. You can use either simple (') or double (") quotes for expressions which contains more than one word. ---- Server IP: 69.147.83.197 Probable Submitter: 86.106.86.140 ---- Manual Page -- http://www.php.net/manual/en/function.preg-split.php Edit -- https://master.php.net/note/edit/92632 Del: integrated -- https://master.php.net/note/delete/92632/integrated Del: useless -- https://master.php.net/note/delete/92632/useless Del: bad code -- https://master.php.net/note/delete/92632/bad+code Del: spam -- https://master.php.net/note/delete/92632/spam Del: non-english -- https://master.php.net/note/delete/92632/non-english Del: in docs -- https://master.php.net/note/delete/92632/in+docs Del: other reasons-- https://master.php.net/note/delete/92632 Reject -- https://master.php.net/note/reject/92632 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158983) next »