Actually, change it to this. It's an incredibly slight change and really doesn't make a difference, but I'm very strict about standards compliance, so I shouldn't be handing out code that's less than pristine. The only thing I changed was to change the \s to \\s, which actually doesn't change the end result whatsoever since \s is not reinterpreted by PHP before sending it to the regex engine, but it's always better to properly use your escape characters.
--------------------------
<?php
$search = $_GET['search'];
$s = new Search();
$terms = $s->parse_search($search);
echo '<pre>' . print_r($terms, true) . '</pre>';
class Search
{
var $terms;
function Search()
{
$this->terms = array();
}
function safe_query($search)
{
return preg_replace('/%|_|\'|\\\\
}
function parse_search($search, $safe = true)
{
$temp = array();
preg_match_all('/"([^"]+)"
for ($i = 1; $i < count($temp); $i++)
{
foreach ( $temp[$i] as $value )
{
if ( strlen($value) >= 3 )
{
$this->terms[] = $value;
}
}
}
return $this->terms;
}
}
?>
Main Topics
Browse All Topics





by: soapergemPosted on 2006-11-12 at 22:33:24ID: 17928176
I made you a class to accomplish this. It should be fairly self-explanatory. First declare a new Search() object, and then use the parse_search() function, which returns an array of all search terms, per your specification. Anything written inside of quotations is a search term, and any word that stands alone outside of quotations is a search term. Plus there is a requirement that they be at least 3 letters in length. So if the string $search == '"big boats" trucks', then the parse_search() function would return an array as follows:
---------- ---------- ---------- ---------- ---------- ----
/', '\\\\$0', stripslashes($search));
|([^\s]+)/ ', (( $safe ) ? $this->safe_query($search) : $search), $temp);
Array
(
[0] => big boats
[1] => trucks
)
I also made the assumption that you would be using this with a MySQL database, so I went ahead and included a function in this class called "safe_query" that will make everything more compatible with MySQL. Enjoy!
--------------------------
<?php
$search = $_GET['search'];
$s = new Search();
$terms = $s->parse_search($search);
// this line is only here for debugging/testing it out
echo '<pre>' . print_r($terms, true) . '</pre>';
class Search
{
var $terms;
function Search()
{
$this->terms = array();
}
function safe_query($search)
{
return preg_replace('/%|_|\'|\\\\
}
function parse_search($search, $safe = true)
{
$temp = array();
preg_match_all('/"([^"]+)"
for ($i = 1; $i < count($temp); $i++)
{
foreach ( $temp[$i] as $value )
{
if ( strlen($value) >= 3 )
{
$this->terms[] = $value;
}
}
}
return $this->terms;
}
}
?>