Dragonasu
Dragonasu

Reputation: 31

Search word in txt file with PHP

I have a txt file and with PHP i need to get [email protected] and [email protected] How can i do it?

I have 2 days searching the solution but i dont find it.

I try this but don't work:

    $filedata = file_get_contents("file.txt");
preg_match_all('~^Email:(.*)$~m', $filedata, $matches);
foreach ($matches[1] as $clave=>$result_email)
        {
        if ($stmt = $mysqli->prepare("SELECT name, email, pass FROM table_bd WHERE email=? ")) {
  
        $stmt->bind_param('s', $result_email);
        $stmt->execute();    // Execute the prepared query.
        $stmt->store_result();
        if ($stmt->num_rows ==1) {
                // get variables from result.
        $stmt->bind_result($name, $email, $pass);
        $stmt->fetch();
        }
         }
       echo $name.":".$pass."<br/>";
    }

I have this txt file:

#name,email/username,password,description,unique name,email validation username
"lyndia.easda,geasdsadrs,,234342:4344,rocasda34,TasdadL,,01/01/1900,""" 
Username:user1

Biography:  

UserId:6333 

Email:[email protected]  

PhoneNumber:+82234614   

"Original number::325 /5ersion/6.0 Mobile/10B350 Safari/8536.25"""
"asdasd.985,asd34e3,,456465:6573,rocsdad7,023sa,,01/01/1900,""" 
Username:user2  
    Biography:  

UserId:392347703    

Email:[email protected]  

PhoneNumber:+823werrs

Upvotes: 0

Views: 63

Answers (2)

Syscall
Syscall

Reputation: 19780

strpos() returns FALSE or 0. It's different : FALSE if not found, 0 at index 0.

You have to use a strict equality to check if it is false or 0 :

if (strpos($line, $word) !== false) 

Will outputs :

Word find: Email: 8

Word find: Email: 19

Another way to get email:

$filedata = file_get_contents("file.txt");
preg_match_all('~^Email:(.*)$~m', $filedata, $matches);
print_r($matches[1]);

Outputs :

Array
(
    [0] => [email protected]
    [1] => [email protected]
)

Upvotes: 1

Mahtab Alam
Mahtab Alam

Reputation: 54

You can use explode() to get Email value as

if (strpos($line, $word) !== false) {
  $email_array = explode(":",fgets($line));
  $email_array[1];//this will give you email id
  echo "<p>Word find: <b> $email_array[1]</b></p>";
  echo $line_count;
}

Upvotes: 0

Related Questions