Checking for a pattern match in a file
This shows how to open a file and check for a pattern match in that file . First create a text file with some entries in it , here is our websites text file .
asp
php
vbscript
javascript
java
html
Now we will try and search for a site in that file
Code :
<?php
//find a match in a file
//our pattern
$pattern = "html";
//open the websites file for reading
if(!$fp = fopen("websites.txt","r"))
{
echo "cannot open the file";
}
else
{
//while the file is not empty
while(!feof($fp))
{
//read a line at a time to try and find a match
$read_line = fgets($fp,255);
if($read_line == $pattern)
echo "We have a match<br>";
}
fclose($fp);
}
?>
Example :
We have a match