note 100166 modified in function.stripos by danbrown
| From: | danbrown@php.net | Date: | Wed, 29 Sep 2010 16:59:18 +0000 |
| Subject: | note 100166 modified in function.stripos by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172818@lists.php.net to get a copy of this message | ||
Just a friendly hint:
I came across to this scenario, check to see if an URL string contains http, if no then prepend
"http" to the URL string
<?php
$url = (stripos($url,"http")==false) ? "http".$url : $url;
?>
now, this will always add the http:// to the URL string even the URL string
already has http
It is because stripos($url,"http") will return false if no http found, and stripos($url,
"http") will return 0 if it is found (being at the start of the string) , and (0==false)
is a true condition
therefore, to fix this problem, we have to use (===) to compare the value type as well:
<?php
$url = (stripos($url,"http")===false) ? "http".$url : $url;
?>
James Lin
--was--
Just a friendly hint:
I came across to this scenario, check to see if an URL string contains http, if no then prepend
"http" to the URL string
$url = (stripos($url,"http")==false) ? "http".$url : $url;
now, this will always add the http:// to the URL string even the URL string
already has http
It is because stripos($url,"http") will return false if no http found, and stripos($url,
"http") will return 0 if it is found (being at the start of the string) , and (0==false)
is a true condition
therefore, to fix this problem, we have to use (===) to compare the value type as well:
$url = (stripos($url,"http")===false) ? "http".$url : $url;
James Lin
http://php.net/manual/en/function.stripos.php