Re: parsing out quoted text
| From: | Stian Berger | Date: | Sun, 11 Jun 2006 20:20:17 +0000 |
| Subject: | Re: parsing out quoted text | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-237770@lists.php.net to get a copy of this message | ||
On Fri, 09 Jun 2006 14:53:09 +0200, sam <php@itab.com> wrote:
$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? thanksI once for just the fun of it, made a regular expression to solve this kind of problem. I haven't tried it in production enviroment, but only on some basic examples. It should support both single and double quotes. $str = 'bass "electric organ" bagpipes'; preg_match_all("/(?<=('|\"))[^\\1]+(?=\\1)|[^ \"']+/",$str,$match); /* $match[0] Array (
[0] => bass
[1] => electric organ
[2] => bagpipes
)
*/
foreach($match[0] as $key => $value) {
$match[0][$key] = 'tb_instr = "'.mysql_escape_string($value).'"';
}
$sql = "SELECT * FROM table WHERE ".implode(' AND ',$match[0]);
print($sql);
//SELECT * FROM table WHERE tb_instr = "bass" AND
//tb_instr = "electric organ" AND tb_instr = "bagpipes"