Search code examples
phpstrpos

Checking if strpos() is truthy fails to find match at start of string


I want to delete word from text if line contain this word for example(if line contain hello I want to delete this line from text) I used this code

$ldap="ldap".$sil;
$lines=array();

foreach(file('/etc/freeradius/sites-enabled/default') as $line){
    if(strpos($line,$ldap)){
        echo "dfasd";
        $line = str_replace($ldap, '', $line);
        array_push($lines, $line);
    }

    array_push($lines, $line);
}

file_put_contents('/etc/freeradius/sites-enabled/default', $lines);

but this code return that it not contain this string but when I write the string directly into strpos for example

strpos($line,'ldap10')

it deleted this line why Strpos not working with variables?


Solution

  • from http://php.net/manual/en/function.strpos.php the function return the position of word if found or return false otherwise.

    so you should change your code to:

    if(strpos($line,$ldap)!== false){}