如何添加文本以防缺少回显$ _GET [复制]

Possible Duplicate:
Add http:// prefix to URL when missing

I'm using this code like this:

<?php echo $_GET['i']; ?>

myweb.com/sample.php?i=http://sample.com

But I want to make sure it always has http:// just in case this is provide myweb.com/sample.php?i=sample.com

How can I make this code <?php echo $_GET['i']; ?> make sure always adds the http:// in case is missing?

You can check, your $_GET['i'] and accept only with http:

For example:

'http://' contains 7 chars + smallest url 'w.xx' contains 4 chars

So, minimal lenght of your $_GET[i] is 11 chars

if(!empty($_GET['i']))
{

    $len = strlen($_GET['i']);
    if($len > 10 and  substr($_GET['i'], 0, 7) === 'http://')
    {     
        [... some of your code ?]
    }
    else if ($len > 3)
    {

        //force add http://
        $_GET['i'] = 'http://'.$_GET['i'];
     }

     unset($len);
}
else
{
    $_GET['i'] = 'No url?';
}

// <-- Place for your echo

Use this way:

<?php
    if (strpos("http://", $_GET['i']) !== 0)
        $_GET['i'] = "http://" . $_GET['i'];
?>

Or as an echo statement:

echo (strpos("http://", $_GET['i']) !== 0) ? "http://" . $_GET['i'] : $_GET['i'];

if you are generating the above mentioned http url dynamically into your query string, use urlencode or rawurlencode functions inside php to concatenate the query string to your url and then accept in request using http get ($_GET['i']).

Hope it works for you.

Use stristr() function.

$url = stristr($_GET['i'],'http://');
if($url != '')

Then string contains 'http'.