strpos
PHP check if file contains a string
Much simpler: <?php if( strpos(file_get_contents(“./uuids.txt”),$_GET[‘id’]) !== false) { // do stuff } ?> In response to comments on memory usage: <?php if( exec(‘grep ‘.escapeshellarg($_GET[‘id’]).’ ./uuids.txt’)) { // do stuff } ?>
How to make strpos case insensitive
You’re looking for stripos() If that isn’t available to you, then just call strtolower() on both strings first. EDIT: stripos() won’t work if you want to find a substring with diacritical sign. For example: stripos(“Leży Jerzy na wieży i nie wierzy, że na wieży leży dużo JEŻY”,”jeży”); returns false, but it should return int(68).
Which method is preferred strstr or strpos?
From the PHP online manual: If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.
Using an array as needles in strpos
@Dave an updated snippet from http://www.php.net/manual/en/function.strpos.php#107351 function strposa($haystack, $needles=array(), $offset=0) { $chr = array(); foreach($needles as $needle) { $res = strpos($haystack, $needle, $offset); if ($res !== false) $chr[$needle] = $res; } if(empty($chr)) return false; return min($chr); } How to use: $string = ‘Whis string contains word “cheese” and “tea”.’; $array = array(‘burger’, ‘melon’, ‘cheese’, ‘milk’); … Read more