Chris Burton
Chris Burton

Reputation: 1205

Remove "http://" from URL string

I am using a bit.ly shortener for my custom domain. It outputs http://shrt.dmn/abc123; however, I'd like it to just output shrt.dmn/abc123.

Here is my code.

//automatically create bit.ly url for wordpress widgets
function bitly()
{
  //login information
  $url = get_permalink();  //for wordpress permalink
  $login = 'UserName'; //your bit.ly login
  $apikey = 'API_KEY'; //add your bit.ly APIkey
  $format = 'json'; //choose between json or xml
  $version = '2.0.1';
  //generate the URL
  $bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$apikey.'&format='.$format;

  //fetch url
  $response = file_get_contents($bitly);
//for json formating
  if(strtolower($format) == 'json')
  {
    $json = @json_decode($response,true);
    echo $json['results'][$url]['shortUrl'];
  }
  else //for xml formatting
  {
    $xml = simplexml_load_string($response);
    echo 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
  }
}

Upvotes: 1

Views: 4945

Answers (3)

Lance Bryant
Lance Bryant

Reputation: 157

You want to do a preg_replace.

$variable = preg_replace( '/http:\/\//', '', $variable ); (this is untested, so you might also need to escape the : character ).

you can also achieve the same effect with $variable = str_replace('http://', '', $variable )

Upvotes: -1

Change your following line:

 echo $json['results'][$url]['shortUrl'];

for this one:

 echo substr( $json['results'][$url]['shortUrl'], 7);

Upvotes: 3

zerkms
zerkms

Reputation: 254886

As long as it is supposed to be url and if there is http:// - then this solution is the simplest possible:

$url = str_replace('http://', '', $url);

Upvotes: 5

Related Questions