如何使用preg_replace在缺少这些子字符串的URL中添加“www”和“http://”?

I want to make this by the preg_replace:

$web = 'example.com';

I want the preg_replace make it http://www.example.com and if it http://example.com no problem. It should add http:// in it found in the URL.

I want do that is http:// not exist.

preg_replace("/^(?:http:\/\/)?(.*)/","http://$1",$web);

You don't need regular expressions for that. Just do $web = "http://$web".

if( 0 !== strpos( $web, 'http://' ) )
{
    $web = 'http://'.$web;
}

Basically, you don't need a regular expression. What that should do is check to see if 'http://' is the first part of $web. If not, it will add 'http://' to the beginning of the string. Otherwise, it does nothing.

Another way to do that is to simply check if it's false... if( false === strpos( $web, 'http://' ) ) That should execute if the function fails. I don't think that's the best way to do it, however.