문제

i'm trying to put a http:// on a string if it does not contain it and then disregard the string if it already does the problem is it does not disregard the string if it does contain it, here is my code:

if(strpos($_POST['test-website'], "http://") === false || strpos($_POST['test-website'], "https://") === false){
    $url = "http://".$_POST['test-website'];
}else
    $url = $_POST['test-website'];

//Value: test.com
//Result: http://test.com

//Value: http://test.com
//Result: http://http://test.com

//Value: https://test.com
//Result: http://https://test.com
도움이 되었습니까?

해결책

You should use === false instead of !== true, strpos will never return true.

if(strpos($_POST['test-website'], "http://") === false || strpos($_POST['test-website'], "https://") === false) {

다른 팁

strpos() Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Returns FALSE if the needle was not found.it wont return true.

if(strpos($_POST['test-website'], "http://") === false || strpos($_POST['test-website'], "https://") === false
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top