php - grab matching words out of the search string -
im trying pull matching words out of search string via $_get['kw'];
function. if person write "i+want+use+google"
, script display "google
"(only when word "google
" inside txt ), if write "i+want+yahoo+to+use
", display "yahoo
"(because "yahoo
" inside txt), , if write "i+want+to+say+hello+to+dr.google
", script display "hello dr.google
" because inside txt have "hello dr.google
" have "google
" inside our query should match "hello dr.google
" looking exact matches...and echo
function use in multiple places triger word $match_term
.
in txt file words grouped this(everything without "."
, /./
|.|
):
yahoo
hello dr.google
other
other
other
othe.r
and file big on 5000 words found bigger files funcion:"fget
" better use...
searched multiple q&a in stackoverflow exact query match function didnt found im looking for.
qestion: how proper code should like?
far found code not looking longer strings...
<?php $file = 'somefile.txt'; $searchfor = $_get['kw']; // file contents, assuming file readable (and exist) $contents = file_get_contents($file); // escape special characters in query $pattern = preg_quote($searchfor, '/'); // finalise regular expression, matching whole line $pattern = "/^.*$pattern.*\$/m"; // search, , store matching occurences in $matches if(preg_match_all($pattern, $contents, $matches)){ echo "found matches:\n"; echo implode("\n", $matches[0]); } else{ echo "no matches found"; }
Comments
Post a Comment