Check if variable starts with 'http'

本秂侑毒 提交于 2019-11-28 11:54:45
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

Use substr:

if (substr($source, 0, 4) === 'http')
if(preg_match('/^(http)/', $source)){
...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!