jopperdepopper Posted March 8, 2010 Share Posted March 8, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/194524-creating-simple-search-option/ Share on other sites More sharing options...
aeroswat Posted March 8, 2010 Share Posted March 8, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/194524-creating-simple-search-option/#findComment-1023101 Share on other sites More sharing options...
Anti-Moronic Posted March 8, 2010 Share Posted March 8, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194524-creating-simple-search-option/#findComment-1023109 Share on other sites More sharing options...
Anti-Moronic Posted March 8, 2010 Share Posted March 8, 2010 Sorry - just to emphasize, my post above is a solution to better managing your article content - not the solution for searching it. Quote Link to comment https://forums.phpfreaks.com/topic/194524-creating-simple-search-option/#findComment-1023110 Share on other sites More sharing options...
sasa Posted March 8, 2010 Share Posted March 8, 2010 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/194524-creating-simple-search-option/#findComment-1023112 Share on other sites More sharing options...
Anti-Moronic Posted March 8, 2010 Share Posted March 8, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/194524-creating-simple-search-option/#findComment-1023114 Share on other sites More sharing options...
sasa Posted March 8, 2010 Share Posted March 8, 2010 | means or in regex Quote Link to comment https://forums.phpfreaks.com/topic/194524-creating-simple-search-option/#findComment-1023132 Share on other sites More sharing options...
Anti-Moronic Posted March 8, 2010 Share Posted March 8, 2010 | means or in regex Sorry, I know what it means - just saying, very novel idea. Quote Link to comment https://forums.phpfreaks.com/topic/194524-creating-simple-search-option/#findComment-1023149 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.