Checking if input number is allready in list file

i tried many methods to check if the given input number/word is allready in the list file where it should have written to… if yes it should not saved in it and if no add to the last line.

see here one of my code try’s
[php]

<?php $file = 'list.html'; $content = file('list.html'); $code = $_POST['data']; $break = "

"; echo " "; if(in_array($code,$content)) { echo "$code allready there"; } else { file_put_contents($file, $code, FILE_APPEND | LOCK_EX); file_put_contents($file, $break, FILE_APPEND | LOCK_EX); echo "$code succesfully added"; } ?>

[/php]

i also tried with array_search and count btw array_unique
no success yet, maybe you can help me.

I haven’t touched files and preg_match concepts in PHP but managed to write the code for you. I hope this is what you intended.

[php]<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
	
	$file = 'list.html';

	/* Creates a handler for the file and opens it in writing mode  “a+” places the pointer at the end of the file */
	$handle = fopen($file, 'a+'); 

	/*fread function stores all the files contents in the $contents variable and filesize() function ensures that the entire file is read */
	$content = fread($handle, filesize($file));
	$break = "<br /><br />";

	/* Querying the $content variable against user supplied value*/
	if (preg_match("/{$_POST['data']}/", $content)){

		echo $_POST['data']. " is already in the List";

	} else {

		fwrite($handle, $_POST['data']);
		fwrite($handle, $break);
		echo $_POST['data']. " was added successfully to the list";

	}

	fclose($handle);
}

 echo "<form action='' method='post'><input name='data' type='text' /><input type='submit' value='add' /></form>";

?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service