[thelist] php: string manipulation question

Dunstan Orchard dunstan at 1976design.com
Mon Sep 29 10:19:03 CDT 2003


Hi there,

Another question based around my search problem, but this time it relates to
building the query.

I'm currently grabbing the search terms the user enters, splitting them at ' ',
inserting the result into an array, and then building the query, thus:

$sterms = trim($HTTP_GET_VARS['searchterms']);
$terms = explode(' ', $sterms);
foreach($terms as $term)
 {
 if ($i == 0)
  {
  $title_vars .= "blog_post_title LIKE '%$term%'";
  $body_vars .= "blog_post_body LIKE '%$term%'";
  }
 else
  {
  $title_vars .= " AND blog_post_title LIKE '%$term%'";
  $body_vars .= " AND blog_post_body LIKE '%$term%'";
  }
 $i++;
}

$query = "SELECT DISTINCT title, body FROM blog_post WHERE (($title_vars) OR
($body_vars))";


This all works fine. However, I'd like people to be able to enter phrases and
single words, eg. 'css "margin left"' into the search box.

The resulting url for that search would look like this:
http://www.foo.com?searchterms=css+%22margin+left%22

I can't for the life of me figure out how to deal with this in PHP though.
My plan was to:

[1] Remove each instance of things enclosed by "" from $sterms and put each
instance into an array called $phrases.
[2] Split the remaining search terms up into the $terms array as before.
[3] Add the two arrays together.
[4] Build the mysql query using this new array as before.

Can anyone tell me if this is a suitable approach, and if so, possibly provide a
clue how I can accomplish step [1]?

It has me stumped :o(

Many thanks - Dunstan

---------------------------
Dorset, England
http://www.1976design.com/
http://www.1976design.com/blog/


More information about the thelist mailing list