mr_burns wrote:
How do I break a string up this way?
$array = explode(" ", $string_with_spaces);
Also, is this the most common way of creating a means of searching a
database?
For basic search engines, it probably is. However, you will loose the
relationship between the words, which is most of the times is implied.
When you enter "red striped top" in a search engine, you would expect to get
the matches with all the words at the top.
This won't happen when you search the database word by word.
A better way is to use a like clause, which can be build quite easily from a
string. Here's an example:
<?php
$s = "red striped top";
$q = "select * from table where";
$array = explode(" ", $s);
$popped = array();
$like = "";
while (count($array) > 1) {
$clause = implode("%", $array);
if ($like) $like .= " or";
$like .= " field like '%$clause%'";
$popped[] = array_pop($array);
}
$q .=
$like .
($like ? " or field like '%" : " field like '%") .
implode(
"%' or field like '%",
array_merge($array, $popped)
) .
"%'"
;
print "<code>$q</code>";
?>
JW