tags:

views:

32

answers:

3

now i want make this by the preg_replace

$web = 'site.com';

i want the preg_replace make it http://www.site.com and if it http://site.com no prblem it whould add http:// in it found in the url thanks

please i want do that is http:// not exsist

A: 

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

Ignas R
no because if it exsist will make it http://http://site.com
moustafa
The code has it is reported would add http:// even when it is already present. I agree there is no need to use regular expressions, in such cases.
kiamlaluno
+3  A: 
preg_replace("/^(?:http:\/\/)?(.*)/","http://$1",$web);
S.Mark
no no no no no if http:// in already exsist will add more one
moustafa
updated - 15char
S.Mark
its not worknig if it exsisthttp://www.site .com/http://
moustafa
added ^ in front
S.Mark
thanks its worked
moustafa
you're welcome -
S.Mark
A: 
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.

Jeff Rupert
thanks it will work good too
moustafa
Rather than using `strstr()`, it would be better to use `strpos()`.
kiamlaluno
@kiamlaluno - Thanks, I always forget about `strpos()`. Updated my answer to include that instead.
Jeff Rupert