How to search for a string in txt file ?

, , , ,

One of our clients asked how to display the hits from his website that are stored in a txt file .

First of all we need to search for that specific link and then display the hits.

This is a part of the hits.txt file

thedomain.com/news/ 232
thedomain.com/pictures/ 574
thedomain.com/company/ 433

And the php solution

// we get the url of the page
$pageURL = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
$page = file_get_contents('hits.txt'); // this is the content of the page
// now we search for the current url
// if the url is found will display the number of hits
if(preg_match('%\b'.preg_quote($pageURL).'\b\s(\d+)%', $page, $display)) {
echo $display[1];
}else{
echo 'No url found';
}

If you need help with your scripts please contact us



Comments are closed.