Ryan
Ryan

Reputation: 27

In-casesensitive PHP search?

So, I'm trying to search for specific words / values in a text file.

Currently, I can only search it case sensitive.

Here's my code:

<html>
<?php
	//$searchthis = "ignore this";
	$matches = array();
	$FileW = fopen('result.txt', 'w');
	$handle = @fopen("textfile.txt", "r");
	ini_set('memory_limit', '-1');
	if ($handle)
	{
		while (!feof($handle))
		{
			$buffer = fgets($handle);
			if(strpos($buffer, $_POST["search"]) != FALSE)
				$matches[] = $buffer;
		}
		fwrite($FileW, print_r($matches, TRUE));
		fclose($handle);
	}
	print 'Found Possible Matches, here are the results' ."\n";
	sleep(2);
	//show results:
	sleep(10);
?>
	</br>
<?php
    $file = "result.txt";
    $text = file_get_contents($file);
    $text = nl2br($text);



    echo $text; 
?>

</html>

So, instead of doing this:

Query for search: casesensiTiveworD Result: Nothing

when there's a value in textfile.txt that's "casesensitiveword"

I want it to do this:

Query for search: casesensiTiveworD

Result: Casesensitiveword

But I don't know how to do this, I'd appreciate any help, I've been trying to figure this out for hours now and I can't.

Upvotes: 0

Views: 33

Answers (1)

Vinod K
Vinod K

Reputation: 148

Replace the following line ...

if(strpos($buffer, $_POST["search"]) != FALSE)

with this one ..

if(stripos($buffer, $_POST["search"]) !== FALSE)

function 'stripos' ignores case sensitivity check

Upvotes: 1

Related Questions