Re: parsing out quoted text

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

 



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


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