Re: parsing out quoted text

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 09 Jun 2006 14:53:09 +0200, sam <php@xxxxxxxx> 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?

thanks

I 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"

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux