...

View Full Version : Quick help



Trel
12-24-2003, 03:22 AM
I need a code snippit to do this



if("http://" is at the begging of the variable $url)
{
do nothing
}
else
{
append to the beginning of the variable $url
}

SDP2006
12-24-2003, 03:27 AM
<?php
$url = "http://www.codingforums.com";
if(preg_match(/http/, $url)){
echo ""; /*Does Nothing*/
}
else
{
/*What do you mean by - "append to the beginning of the variable $url"*/
}
?>

What do you mean by - "append to the beginning of the variable $url"??

Hope this helps.

Trel
12-24-2003, 03:32 AM
hmmm I'm not sure if that would work

I want it to check to see if "http://" (<-- the whole thing) is at the beginning of $url and if not, add it

so if $url = http://www.site.com, it's left alone but
if $url = www.anothersite.com, it's changed to http://www.anothersite.com

raf
12-24-2003, 09:03 AM
you could use regex, but i think a simple substr will be more effincient

so


if (substr($url,0,7) != "http://"){
$newurl = "http://" ;
}
$newurl .= $url ;


should do the trick

bcarl314
12-24-2003, 01:19 PM
or



preg_match("/http:\/\//i",$url);

mordred
12-24-2003, 04:12 PM
Better check for



if ( !preg_match("/^http:\/\//i",$url) ) { // etc.


since it's only important to know if "http" is at the beginning of the string, not somewhere else.

Trel
12-24-2003, 08:00 PM
Originally posted by raf
you could use regex, but i think a simple substr will be more effincient

so


if (substr($url,0,7) != "http://"){
$newurl = "http://" ;
}
$newurl .= $url ;


should do the trick

That one worked and I modified it just a little bit, just changed it not to use the $newurl variable :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum