Question

I want to clean the url addres but first I want to check if what should be replace is in the string. I want to use strpos because is faster, but it fails to detect the string.

$txt = 'http://www.youtube.com/watch?v=aWFmXFFjJfw';

if(strpos($txt, 'http://www.')){
echo 'true strpos'; // not shows
}

if(strstr($txt, 'http://www.')){
echo 'true strstr'; // show
}

Also

$match = strpos($txt, 'http://www.');
var_dump($match); // int(0);
Was it helpful?

Solution

Because it equals 0 which equates to false.

Instead use:

if(strpos($txt, 'http://www.')!==false){
    echo 'true strpos'; //shows
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top