[thelist] php: string manipulation question

Javier Muniz jmuniz at granicus.com
Mon Sep 29 12:19:45 CDT 2003


Why not use a fulltext search?  MySQL has fulltext search built in, as I
imagine some other db's would.  Alternatively you can use a fulltext
indexing solution, but you probably don't want to implement a fulltext
search by executing potentially large numbers of SQL queries and then
figuring out what to do with all the results.

-Javier

-----Original Message-----
From: Dunstan Orchard [mailto:dunstan at 1976design.com] 
Sent: Monday, September 29, 2003 8:19 AM
To: evolt list
Subject: [thelist] php: string manipulation question


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/
-- 
* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester 
and archives of thelist go to: http://lists.evolt.org 
Workers of the Web, evolt ! 


More information about the thelist mailing list