Search String and Return Line PHP [duplicate]
Asked Answered
K

5

6

I'm trying to search a PHP file for a string and when that string is found I want to return the whole LINE that the string is on. Here is my example code. I'm thinking I would have to use explode but cannot figure that out.

$searchterm =  $_GET['q'];

$homepage = file_get_contents('forms.php');

 if(strpos($homepage, "$searchterm") !== false)
 {
 echo "FOUND";

 //OUTPUT THE LINE

 }else{

 echo "NOTFOUND";
 }
Kimsey answered 15/3, 2012 at 14:34 Comment(0)
O
20

Just read the whole file as array of lines using file function.

function getLineWithString($fileName, $str) {
    $lines = file($fileName);
    foreach ($lines as $lineNumber => $line) {
        if (strpos($line, $str) !== false) {
            return $line;
        }
    }
    return -1;
}
Ozan answered 15/3, 2012 at 14:48 Comment(5)
Short circuits on the first occurence of the magic string. Also does not return the line, only the line number that the string happened on.Jocund
I was able to get the contents of that line by echoing the $line. Works great!Kimsey
Ou, sorry I thought you wanted just the line number, now it's fixed.Ozan
@Martin What if I need not the current line content but the next line content? cheers :)Divisionism
This will be problematic if the file is giganticStalemate
C
2

You can use fgets() function to get the line number.

Something like :

$handle = fopen("forms.php", "r");
$found = false;
if ($handle) 
{
    $countline = 0;
    while (($buffer = fgets($handle, 4096)) !== false)
    {
        if (strpos($buffer, "$searchterm") !== false)
        {
            echo "Found on line " . $countline + 1 . "\n";
            $found = true;
        }
        $countline++;
    }
    if (!$found)
        echo "$searchterm not found\n";
    fclose($handle);
}

If you still want to use file_get_contents(), then do something like :

$homepage = file_get_contents("forms.php");
$exploded_page = explode("\n", $homepage);
$found = false;

for ($i = 0; $i < sizeof($exploded_page); ++$i)
{
    if (strpos($buffer, "$searchterm") !== false)
    {
        echo "Found on line " . $countline + 1 . "\n";
        $found = true;
    }
}
if (!$found)
    echo "$searchterm not found\n";
Consistory answered 15/3, 2012 at 14:41 Comment(2)
What is the 4096 for?Rider
@JohnMagnolia php.net/manual/en/function.fgets.php - It's the buffer size. It will read 4096 -1 bytes.Consistory
B
1

If you use file rather than file_get_contents you can loop through an array line by line searching for the text and then return that element of the array.

PHP file documentation

Bombast answered 15/3, 2012 at 14:40 Comment(0)
J
1

You want to use the fgets function to pull an individual line out and then search for the

<?PHP   
$searchterm =  $_GET['q'];
$file_pointer = fopen('forms.php');
while ( ($homepage = fgets($file_pointer)) !== false)
{
    if(strpos($homepage, $searchterm) !== false)
    {
        echo "FOUND";
        //OUTPUT THE LINE
    }else{
    echo "NOTFOUND";
    }
}
fclose($file_pointer)
Jocund answered 15/3, 2012 at 14:40 Comment(0)
P
1

Here is an answered question about using regular expressions for your task.

Get line number from preg_match_all()

Searching a file and returning the specified line numbers.

Pinniped answered 15/3, 2012 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.