Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PHP strpos not case sensitive?

I have made a php script that searches a whole directory with text files in it, everything works fine, except that it is case sensitive. How would i search without it being case sensitive? Here's the code i have so far:

<?php
    $query = $_POST['search'];
    if ($handle = opendir('posts')) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != "..") {

                echo "<p>$entry ";
                $file = file_get_contents('posts/'.$entry, FILE_USE_INCLUDE_PATH);
                $check_str = strpos($file,$query);

                if ($check_str == 0) {
                    print "not found</p>";
                } else {
                    print "found</p>";
                }

            }
        }
        closedir($handle);
    }
?>
like image 793
Jacoby Yarrow Avatar asked Jul 18 '15 02:07

Jacoby Yarrow


People also ask

Is Strpos case-insensitive?

Note: The stripos() function is case-insensitive.

How can I compare two strings without case-sensitive in PHP?

The strcasecmp() function compares two strings. Tip: The strcasecmp() function is binary-safe and case-insensitive. Tip: This function is similar to the strncasecmp() function, with the difference that you can specify the number of characters from each string to be used in the comparison with strncasecmp().

What is not case-sensitive in PHP?

In PHP, class names as well as function/method names are case-insensitive, but it is considered good practice to them functions as they appear in their declaration.

Is PHP keyword case-sensitive?

No, Keywords are not case-sensitive.


1 Answers

Yeah, stripos() is what you're looking for. Here's the manual page.

like image 164
blasko Avatar answered Oct 18 '22 11:10

blasko