Hi friends ..

I have a mailing_list file ..

I wanted to check whether the visitor entered Email already exists in file?

Is there any standard function for it ?

7 Years
Discussion Span
Last Post by Shankye

Quite a few ways to do this. A trivial way would be:

1) place file into a variable (string) with file_get_contents().
2) check string for the email by using stripos().

** For checking with stripos - you MUST use the ' === false ' comparison, otherwise you could get the wrong result. See the php manual for details.

However, regex may be a better option.

Edited by diafol: n/a


Thank you Ardav ...

I used almost similar method..


$string_Array = file(File_name) ;
$key = 0;

		if( trim($strings[$key]) == $email)
			header("Location: emailUsed.html");

// continue to store email-ID in mail_list ...

Its working for now ..

Any suggestion on it?

Edited by Shankye: n/a


Performance wise, you should avoid loading an entire file into memory when possible.
The following code relies on the SPL (standard php library) which is mostly > php 5.1
Although this can also be done using the file functions in php.

$email = 'user@domain.com';

$file = new SplFileObject('filename.txt');

$match = false;
foreach($file as $line){
	if( false !== stripos( $line, $email ) ){
		$match = true;

if( true === $match ){
	//we found a match
} else {
	//No Match

This reads through a file while only having a single line in memory at a time.
If/when it finds a match, it sets $match = true, you could implement this however you would like, but then breaks out of the foreach loop to stop from having to check the rest of the file.

If you're using a version of php that supports the SplFileObject you should really check it out. http://php.net/manual/en/class.splfileobject.php

Votes + Comments
Thank you for help

Thank you soooooo much mschroeder..
It helped a lot ...
Its really faster now..

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.