strpos

php 5 strpos() difference between returning 0 and false?

不羁的心 提交于 2019-11-26 13:51:30
if(strpos("http://www.example.com","http://www.")==0){ // do work} I'd expect this to resolve as true, which it does. But what happens when I do if(strpos("abcdefghijklmnop","http://www.")==0){// do work} This also passes on php 5 because as far as I can work out the strpos returns false which translates as 0. Is this correct thinking/behaviour? If so what is the workaround for testing for that a substring is at the beginning of another string? 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

Which method is preferred strstr or strpos?

对着背影说爱祢 提交于 2019-11-26 13:21:39
问题 I noticed a lot of developers are using both strstr and strpos to check for a substring existence. Is one of them preferred and why ? 回答1: 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. 回答2: Here are some other answers (+benchmarks) I got to my question, which is almost the same (I didn't realize yours when asking). In the meantime I also made my own benchmark test,

Simple PHP strpos function not working, why?

青春壹個敷衍的年華 提交于 2019-11-26 12:29:57
Why isn't this standalone code working: $link = 'https://google.com'; $unacceptables = array('https:','.doc','.pdf', '.jpg', '.jpeg', '.gif', '.bmp', '.png'); foreach ($unacceptables as $unacceptable) { if (strpos($link, $unacceptable) === true) { echo 'Unacceptable Found<br />'; } else { echo 'Acceptable!<br />'; } } It's printing acceptable every time even though https is contained within the $link variable. 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(

How to make strpos case insensitive

笑着哭i 提交于 2019-11-26 08:20:29
问题 How can I change the strpos to make it non case sensitive. The reason is if the product->name is MadBike and the search term is bike it will not echo me the link. My main concern is the speed of the code. <?php $xml = simplexml_load_file(\'test.xml\'); $searchterm = \"bike\"; foreach ($xml->product as $product) { if (strpos($product->name, $searchterm) !== false ) { echo $product->link; } } ?> 回答1: You're looking for stripos() If that isn't available to you, then just call strtolower() on

PHP check if file contains a string

那年仲夏 提交于 2019-11-26 05:29:57
问题 I\'m trying to see if a file contains a string that is sent to the page. I\'m not sure what is wrong with this code: ?php $valid = FALSE; $id = $_GET[\'id\']; $file = \'./uuids.txt\'; $handle = fopen($file, \"r\"); if ($handle) { // Read file line-by-line while (($buffer = fgets($handle)) !== false) { if (strpos($buffer, $id) === false) $valid = TRUE; } } fclose($handle); if($valid) { do stufff } 回答1: Much simpler: <?php if( strpos(file_get_contents("./uuids.txt"),$_GET['id']) !== false) { //

php 5 strpos() difference between returning 0 and false?

一笑奈何 提交于 2019-11-26 03:44:54
问题 if(strpos(\"http://www.example.com\",\"http://www.\")==0){ // do work} I\'d expect this to resolve as true, which it does. But what happens when I do if(strpos(\"abcdefghijklmnop\",\"http://www.\")==0){// do work} This also passes on php 5 because as far as I can work out the strpos returns false which translates as 0. Is this correct thinking/behaviour? If so what is the workaround for testing for that a substring is at the beginning of another string? 回答1: Yes, this is correct / expected

Simple PHP strpos function not working, why?

旧街凉风 提交于 2019-11-26 02:24:40
问题 Why isn\'t this standalone code working: $link = \'https://google.com\'; $unacceptables = array(\'https:\',\'.doc\',\'.pdf\', \'.jpg\', \'.jpeg\', \'.gif\', \'.bmp\', \'.png\'); foreach ($unacceptables as $unacceptable) { if (strpos($link, $unacceptable) === true) { echo \'Unacceptable Found<br />\'; } else { echo \'Acceptable!<br />\'; } } It\'s printing acceptable every time even though https is contained within the $link variable. 回答1: When in doubt, read the docs: [strpos] Returns the

Using an array as needles in strpos

删除回忆录丶 提交于 2019-11-26 01:42:28
问题 How do you use the strpos for an array of needles when searching a string? For example: $find_letters = array(\'a\', \'c\', \'d\'); $string = \'abcdefg\'; if(strpos($string, $find_letters) !== false) { echo \'All the letters are found in the string!\'; } Because when using this, it wouldn\'t work, it would be good if there was something like this 回答1: @Dave an updated snippet from http://www.php.net/manual/en/function.strpos.php#107351 function strposa($haystack, $needles=array(), $offset=0)