Jump to content

Creating simple search option


jopperdepopper

Recommended Posts

Hi,

 

I want to create a simple search option for a site: I would like a 'google-style' list of results. If I search for 'php mysql forum', I want something like below as the result of the search:

"Along with the tutorials, the developers on the forum will be able to help you ... This covers updating your Dev server to recent versions of PHP and MySQL, ..."

 

 

All my content is stored in a database, in a quick-and-dirty way: a full article is stored in 1 field.

I know how to search for a word in those articles, but when I get the results from the search, I end up with a list of full articles, that could have multiple occurrences of a word in it.

 

I'm not sure how to 'filter inside' the article for so I can present  the result in a decent way.

 

I'm afraid I need RegExp stuff, which I'm not good at  :-[ Any other way for this?

 

 

Any and all suggestions would be great :)

Cheers!

 

Link to comment
Share on other sites

Hi,

 

I want to create a simple search option for a site: I would like a 'google-style' list of results. If I search for 'php mysql forum', I want something like below as the result of the search:

"Along with the tutorials, the developers on the forum will be able to help you ... This covers updating your Dev server to recent versions of PHP and MySQL, ..."

 

 

All my content is stored in a database, in a quick-and-dirty way: a full article is stored in 1 field.

I know how to search for a word in those articles, but when I get the results from the search, I end up with a list of full articles, that could have multiple occurrences of a word in it.

 

I'm not sure how to 'filter inside' the article for so I can present  the result in a decent way.

 

I'm afraid I need RegExp stuff, which I'm not good at  :-[ Any other way for this?

 

 

Any and all suggestions would be great :)

Cheers!

 

You could just use str_replace against each word in the search string. First use explode with a space delimiter in your search string to break the string into seperate words. Then use a loop through that array to go through and bold each word in the topic that matches the searched for word. Something like this (pseudo code)

 

$searchstr = "This that and the other";
$str = substr($str,0,however big you want it)
$arr = array();

$arr[] = explode(' ',$searchstr)
foreach($arr as $val) {
  str_replace($val,"<b>" . $val . "</b>",$str); 
}

echo $str;

Link to comment
Share on other sites

Hi,

 

I want to create a simple search option for a site: I would like a 'google-style' list of results. If I search for 'php mysql forum', I want something like below as the result of the search:

"Along with the tutorials, the developers on the forum will be able to help you ... This covers updating your Dev server to recent versions of PHP and MySQL, ..."

 

 

All my content is stored in a database, in a quick-and-dirty way: a full article is stored in 1 field.

I know how to search for a word in those articles, but when I get the results from the search, I end up with a list of full articles, that could have multiple occurrences of a word in it.

 

I'm not sure how to 'filter inside' the article for so I can present  the result in a decent way.

 

I'm afraid I need RegExp stuff, which I'm not good at  :-[ Any other way for this?

 

 

Any and all suggestions would be great :)

Cheers!

 

Well, as you know, this really is quick and dirty and not at all ideal or scalable. It will cause issues down the line so before I give you an idea of how to do it this way - I must advise you do it the standard way, by separating common information into several fields.

 

You can do this with regex, but if you can change the content structure of how your complete articles are stored and retrieved, you can use explode, then refer to the article via an array like so:

 

FULL ARTICLE:

 

This is the title ||
This is some author info ||
This is the main body of the article
which will go on and on and on
..and on and on ||
This is the footer of the article ||

 

..then we are going to separate them into array items using explode and the delimiter ||:

 

$articledata = the field from your database with the full article content

$article = explode('||', $articledata);

print_r($article);

 

Again, not the most ideal way and of course you will have to use the array to output content or use regex to remove the || delimiter. If you choose to remove the delimiter, do this:

 

$article = preg_replace("#\|\|#", "", $articledata);

 

Then you can safely output using echo $article. OR you cold use a explode and implode like so (should work..eek):

 

$article = explode('||', $articledata);

$article = implode('||', $articledata);

 

.which will give you article as a string without the ||

 

-----

 

Hope that helps.

 

Link to comment
Share on other sites

try

<?php
$test = "Along with the tutorials, the developers on the forum will be able to help you ... This covers updating your Dev server to recent versions of PHP and MySQL, ...";
$srch = "php mysql forum";
$srch = trim($srch);
$srch = preg_replace('/\s+/','|',$srch); //replace spaces with |
$test = preg_replace('/('.$srch.')/i', '<b>$1</b>', $test);
echo $test;
?>

Link to comment
Share on other sites

try

<?php
$test = "Along with the tutorials, the developers on the forum will be able to help you ... This covers updating your Dev server to recent versions of PHP and MySQL, ...";
$srch = "php mysql forum";
$srch = trim($srch);
$srch = preg_replace('/\s+/','|',$srch); //replace spaces with |
$test = preg_replace('/('.$srch.')/i', '<b>$1</b>', $test);
echo $test;
?>

 

Wow, very smart idea replacing spaces with |. Would have never thought of that. Guess that's why I always listen to you ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.