I have a PHP file which will search in txt file and will display the result.
However, if the word is written with uppercase text and the user searches for the same word with lowercase the PHP file will show found no matches1
for example:
I have Apple Juice in the txt file. the user searches for apple juice. the PHP shows no matches found as it is looking for the exact same word "Apple Juice" which has uppercase in it.
This is my code:
<html>
<head><title>some title</title></head>
<body>
<?php
if(!empty($_POST['search'])) {
$file = 'mytxtfile.txt';
$searchfor = '';
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
$searchfor = $_POST['search'];
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$fullword = '\b\Q' . $w . '\E\b';
$regex = '/' . $fullword . '(?!.*' . $fullword . ')/i';
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all("/\b([a-z]+[A-Z]+[a-zA-Z]*|[A-Z]+[a-z]+[a-zA-Z]*)\b/", $pattern, $contents, $matches)){
echo "Population:
";
echo implode("
", $matches[0]);
}
else{
echo "No matches found";
}
header('Content-Type: text/html');
}
?>
<form method="post" action="">
<input type="text" name="search" />
<input type="submit" name="submit" />
</form>
</body>
</html>
i did try and added this if(preg_match_all("/\b([a-z]+[A-Z]+[a-zA-Z]*|[A-Z]+[a-z]+[a-zA-Z]*)\b/",
to my code but that didn't work!
any help would be appreciated.
Thanks