Pages

Tuesday, October 16, 2012

Cara mencari kata dalam file txt


Silahkan copy script dibawah ini simpan dengan nama  test.php



<?php
//fungsi untuk mencari kata dalam file txt
function cek_backup($search,$check_file_backup)
{
    // the following line prevents the browser from parsing this as HTML.
    header('Content-Type: text/plain');
    // get the file contents, assuming the file to be readable (and exist)
    $contents = file_get_contents($check_file_backup);
    // escape special characters in the query
    $pattern = preg_quote($search, '/');
    // finalise the regular expression, matching the whole line
    $pattern = "/^.*$pattern.*\$/m";
    // search, and store all matching occurences in $matches
    if(preg_match_all($pattern, $contents, $matches)){
      $hasil=1;
    }
    else{    
         $hasil=0;
    }
    return $hasil;
}



//path dimana file txt berada (server linux) anda bisa tentukan sendiri
$check_file_backup = '/var/www/trunk/scan/backup.txt';
//kata yang di search
$search = "2012-01-01_sore";   //mencari kata  "2012-01-01_sore" dalam file txt backup.txt
//check dengan memanggil function cek_backup      
$hasil=cek_backup($search,$check_file_backup);

if ($hasil==1) {
 echo "data ada";
}
else {
 echo "data tidak ditemukan";
}
?>




No comments:

Post a Comment