i have been over so many posts online trying to sort this….
i am integrating a small search feature into a php script….
i have tried many scripts including in_array (only works for full match) and array_search
and nothing seems to work….
i have a script that creates an array with file names (all in the format “Random-Name-1.ext”)
but the script removes the file extention, just leaving the filenames….
all filenames are seperated word by word with a -
all words have the first letter capital….
the array is called $files1;
the search string is called $search_string;
what im looking for would be along the lines of a foreach loop to check if the search string is contained in any part of each array value, and if it is, put the full array value into another array called $search_results
as the next part of my script to paginate needs the array $search_results to echo each of the filenames and display them 10 per page….
hope this is enough info, ive been workin on it for ages and racking my brains trying to find the correct code….
thanks in advance
……….
………..
EDIT…..
…….
……..
got script working with preg_grep….. but i now have a slight problem….
the script before the search code is designed to get the url of the search page with q={search string}
and then trim this so that i just have the serach string as a variable…..
i use an str_replace to change the page url from
/games-search?q={search-string}
to {search-string}
this is perfectly fine, but the script to paginate the results as 10 per page adds ?page=2 to the url….
so when i click page 2, the str_replace to change the url to search string doesnt work now, as the new page url for page 2 is
/games-search?page=2&q={search-string}
i have been trying to do another str_replace to change the consecutive pages urls to just the search string but i am having problems with the regex to define the page number…..
the page numbers vary from 1 – about 50 (never more than 99, so 2 digits would be enough to match….
i have tried over and over again today to get this regex correct but i am not sure if i am going about it the correct way…..
here is my latest effort….
/games-search?page=(^[0-9][0-9]+)&
that is what i am trying to replace with “” (eg, nothing)
as i only need the data from after the & character in the url … and thats IF the url even contains this (for exapmple the first page when the url doesnt contain the & character – if the url doesnt contain the & character , i dont want it modifying as i already have the data i need)
thanks again if anyone can help
decided not to paginate the results of each search as ther will never be more than about 15 results….. this is fine, but i am going to implement a miminum search length of 4 characters so it doesnt bring up 500 results for the letter A 🙂
Search an array for a substring match? preg_grep
Then do something like
Then
print_rthat array, and you’ll see you’ve got something dead easy to work with.