PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.example.com/');
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
// Another example, let's get a web page into a string. See also file_get_contents().
$html = implode('', file('http://www.example.com/'));
?>
The code above will put your file into a PHP array. You will need to loop through the array or use and array function such as array search to to find the text.
$search = 'some string';
//when using against actual file uncomment this and comment other $file
//$file = file('/path/to/file.txt');
//just to test without file
$file = array('some string', 'blah', 'blah2', 'some string will still match this');
foreach ($file as $ln => $content) {
if (strpos($content, $search) !== false) {
echo 'Line #'.$ln.' match. <br />';
}
}