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.
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.
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).
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 } ?>
Yes, this is correct / expected behavior : strpos can return 0 when there is a match at the beginning of the string and it will return false when there is no match The thing is you should not use == to compare 0 and false ; you should use ===, like this : if(strpos(“abcdefghijklmnop”,”http://www.”) … Read more
When in doubt, read the docs: [strpos] Returns the numeric position of the first occurrence of needle in the haystack string. So you want to try something more like: // … if (strpos($link, $unacceptable) !== false) { Because otherwise strpos is returning a number, and you’re looking for a boolean true.
@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