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 ?

Member Avatar for diafol

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.

Thank you Ardav ...

I used almost similar method..

used

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

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

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

Its working for now ..

Any suggestion on it?

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.

<?php
$email = 'user@domain.com';

$file = new SplFileObject('filename.txt');
$file->setFlags(SplFileObject::DROP_NEW_LINES);

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

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

commented: Thank you for help +1

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.