$array = array("apple", "banana", "orange", "kiwi"); $keyword = "orange"; $key = array_search($keyword, $array); if ($key !== false) { echo "The keyword was found at index $key"; } else { echo "The keyword was not found in the array"; }
$string = "The quick brown fox jumps over the lazy dog"; $pattern = "/brown/i"; // i for case-insensitive search if (preg_match($pattern, $string)) { echo "Pattern found in the string"; } else { echo "Pattern not found in the string"; }
// Connect to the database $dsn = "mysql:host=localhost;dbname=test"; $user = "username"; $pass = "password"; $conn = new PDO($dsn, $user, $pass); // Search for keywords in the "products" table $keyword = "apple"; $stmt = $conn->prepare("SELECT * FROM products WHERE name LIKE ? OR description LIKE ?"); $stmt->execute(array("%$keyword%", "%$keyword%")); $results = $stmt->fetchAll(); if (count($results) > 0) { echo "Found " . count($results) . " results"; } else { echo "No results found"; }Package library: The PHP search function is built into the core PHP language and does not require any external package or library. However, there are several third-party libraries and frameworks, such as Laravel Scout, Sphinx, and Elasticsearch, that provide more advanced and efficient search capabilities for large-scale applications.