Reputation: 33
So I have
$q = $_REQUEST["q"];
$f = fopen("address.txt", "r");
while (($line = fgets($f)) !== FALSE) {
if (strstr($line, $q)) {
print "<li>found: $line";
Lets say I have
Street 1 number 122
Street 2 number 22
in address.txt
and I want to search for the number 22
.
How to search for 22
, but not to get 122
also?
Thanks in advance.
Upvotes: 0
Views: 60
Reputation: 23892
You could use a regex with word boundaries to accomplish this. Quick example:
if(preg_match('/\b22\b/', $line)) {
PHP Demo: https://eval.in/817207
Regex Demo: https://regex101.com/r/y9slbd/1/
Example using input (the $q
):
if(preg_match('/\b' . preg_quote($q, '/') . \b/', $line)) {
http://php.net/manual/en/function.preg-quote.php
(Although your example of 22
shouldn't have caused any issues if concatenated into this example)
Upvotes: 2