Code di bawah digunakan untuk mencari text/value string pada sebuah file , jika value ditemukan maka akan mereturn line number pada value tersebut ..
<?php
function find_line_number_by_string($filename, $search,$case_sensitive = false)
{
$line_number = "";
if ($file_handler = fopen($filename, "r")) {
$i = 0;
while ($line = fgets($file_handler)) {
$i++;
//case sensitive is false by default
if ($case_sensitive == false) {
$search = strtolower($search); //convert file and search string
$line = strtolower($line); //to lowercase
}
//find the string and store it in an array
if (strpos($line, $search) !== false) {
$line_number .= $i . ",";
}
}
fclose($file_handler);
} else {
return "File not exists, Please check the file path or filename";
}
//if no match found
if (count($line_number)) {
return substr($line_number, 0, -1);
} else {
return "No match found";
}
}
//use function
$output = find_line_number_by_string('example.html', 'materials');
print "String(s) found in ".$output;
0 Comments