Re: parsing out quoted text

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

 



sam 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?

The following was just pushed out of my rear end at great personal expense and may not cover all possibilities, but should be enough for you to build on...

<?php
$str = 'bass "electronic organ" bagpipes';

preg_match_all('/\"(.*?)\"/i', $str, $matches);

$words = array();
foreach ($matches[0] as $match)
{
       $words[] = substr($match, 1, -1);
       $str = str_replace($match, '', $str);
}

foreach (explode(' ', $str) as $word)
{
       $word = trim($word);
       if (strlen($word) > 0)
               $words[] = $word;
}

print_r($words);
?>


Also, your SQL query will not work. tb_instr *cannot* be equal to all three strings at the same time.You either want to be using OR not AND, or you should be using LIKE instead of = with some %s thrown into the strings for good measure. Check the MySQL manual for details.

-Stut

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