Check if variable starts with 'http' Check if variable starts with 'http' php php

Check if variable starts with 'http'


if (strpos($source, 'http') === 0) {    $source = "<a href=\"$source\">$source</a>";}

Note I use ===, not == because strpos returns boolean false if the string does not contain the match. Zero is falsey in PHP, so a strict equality check is necessary to remove ambiguity.

Reference:

http://php.net/strpos

http://php.net/operators.comparison


You want the substr() function.

if(substr($source, 0, 4) == "http") {   $source = "<a href='$source'>$source</a>";}


if(strpos($source, 'http') === 0)    //Do stuff