This code seems to work on a simpler level but not on the one I'm trying to use. It's for a website's search engine.

First the result is gotten from the database like so:

$query = "select * from $tbl_name where name like \"%$var%\" OR text like \"%$var%\""; 


  if ($num == 0) //if no results found
  "No results found";
 else {

Then the result is gotten from the database and matched, it's intended to match all the loosely, everything in the array $skip can be skipped over while matching and then the matched terms are highlighted.

$skip = array(" ","-","_","'"); //ignore
  while ($row = mysql_fetch_array($result)) { 
  $name = $row['name'];
  $text = nl2br($row['text']);

  /*stop input's ($var) casing from affecting the output's casing, skip over chars specified in $skip*/
  $name = preg_replace('/([?]*' .  str_replace('@@@', "['\s_-]*",str_replace($skip,'@@@',$var)) . '[?]*)/i', '<b>${1}</b>', $name); 
  $text = preg_replace('/([?]*' .  str_replace('@@@', "['\s_-]*",str_replace($skip,'@@@',$var)) . '[?]*)/i', '<b>${1}</b>', $text);
echo $name . $text;

The above matches substrings (case insensitive) but doesn't skip the terms listed i.e. for $text to be "hello world", "hello" or "world" would be recognised but not "hello-world" which should.

If the same matching is done on a simpler level then it works. By simpler level I mean another smaller script which runs on the same principle for instance the following, which works, which is pretty much the same thing I'm doing (unless I'm missing something)

$words = "hEllO wOrLD"; 
$skip = array(" ","-","_","'");
$phrase = "I made a program helloworld which prints Hello World hello";
echo preg_replace('/([?]*' .  str_replace('@@@', "['\s_-]*",str_replace($skipchars,'@@@',$words)) . '[?]*)/i', '<b>${1}</b>', $phrase);

Edited by asif49: n/a

6 Years
Discussion Span
Last Post by the_traveller

That could seriously not be more cryptic, do you or someone else who understands that^^ want to explain it to me in a bit more depth ?


It appears that the full-text search method is REALLY slow, as far as everyone is saying. Also that site doesn't give any examples as to how to put it into the code.

I'm hoping there's a way to correct the error I'm having rather than do that full-text-search stuff.

Hopefully someone sees what's wrong with the code and gets back to me about it...


if you consider to change ur mind about using full text search
after adding new full text key with the fields name , text
ur statment should be like this

SELECT $tbl_name ,MATCH (`name`,`text`)AGAINST ('%$var%') AS score
FROM $tbl_name AS $tbl_name
WHERE MATCH (`name`,`text`)AGAINST ('%$var%') 

you should try this in phpmyadmin and check out the results

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.