Re: parsing out quoted text
| From: | nicolas figaro | Date: | Fri, 09 Jun 2006 13:14:42 +0000 |
| Subject: | Re: parsing out quoted text | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-237687@lists.php.net to get a copy of this message | ||
sam a écrit :
$str='bass "electric organ" bagpipes'; $parser($str); $query="SELECT * FROM table WHERE tb_instr = "bass" AND tb_instr = "electric organ" //<<quoted phrase AND tb_instr = "bagpipes"; Anybody know where I can just copy code that will do the above? you wish to split $str and keep what's between the " intact ?you can split with spaces, then parse the array and treat difefrently if the first character of your element is a ". $query = "SELECT *FROM table WHERE "; $array = split ($str," "); foreach ($array as $element) { // "element if ( substr($element,0,1) == "\"" ) { $quoted_element = $element; } // element" elseif ( substr($element,strlen($element)-1,1) == "\"" ) { $query .= "tb_instr = \"".$quoted_element." ".$element."\" AND ";
$quoted_element = "";} // element after "element elseif ($quoted_element != "" ) { $quoted_element .= " ".$element; } else { $query .= "tb_instr = \"".$element."\" AND "; } } // remove the last AND // please check if 4 or 5 characters to remove, 5 should be fine $query = substr ($query,0,strlen($query)-5); print $query; N F
thanks