As I mentioned in the comments your regex was missing the delimters
$fl_array = preg_grep("[A-Z]HITEBOARDS", $array);
Should be
$fl_array = preg_grep("/[A-Z]HITEBOARDS/", $array);
You may or should include the word boundary \b
before and after a word, this will prevent matching partial words such as (for example) if you had this /\b[A-Z]here\b/
which could match therefore
instead of just there
. Without the boundaries matches could happen in the start, middle or end of partial words, which is probably not what you want. The boundary will match anything that is \W
or in other words not \w
or simpler [^a-z0-9_]
or in English: matches anything not alpha, number or the underline, basically all your punctuation , special chars (except _
) and whitespaces.
So to put that in code would be this:
$fl_array = preg_grep("/\b[A-Z]HITEBOARDS\b/", $array);
Also instead of:
$array = explode("
", file_get_contents('test.txt'));
You can use
$array = file('test.txt', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
The file
function is preferred because it breaks the file into an array based on the line endings (not dependent on OS
vs
as explode is). Besides that an better performance it also has two really useful flags. FILE_IGNORE_NEW_LINES
is a given as this removes the line endings which are normally retained in the array by file()
. The FILE_SKIP_EMPTY_LINES
will do basically what it says and skip lines that are empty.
Cheers.