2012-09-13 6 views
1

에서 나는 내 사용자 지정 도메인에 대한 bit.ly 축약 서비스를 사용하고 있습니다. 출력은 http://shrt.dmn/abc123입니다. 그러나, 그냥 출력 shrt.dmn/abc123 싶습니다.제거에 "http : //"URL 문자열

여기 내 코드입니다.

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

의 경우 : 다음 줄을 변경

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

답변

3

: 다음이 솔루션은 가능한 간단합니다 -만큼이 URL을 있어야하고 http://이 있으면대로

//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; 
    } 
} 
+0

매력처럼 작동했습니다. 고맙습니다, @ 넬슨! –

+0

당신은 오신 것을 환영합니다! :-) – Nelson

+0

https? – wesside

5

이 하나

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

이 zerkms을 주셔서 감사합니다. 나는 PHP에 관해서 완전히 지식이 없다. 해당 코드 줄은 어디에 두어야합니까? –

+0

@Christopher 버튼 : 아마도 $ URL을 = get_permalink'후(); '라인 – zerkms

+0

지금은 아무것도 출력하지 않습니다. –

-1

preg_replace를 원합니다.

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

당신은 또한 $ 변수와 동일한 효과를 얻을 수 = 않는 str_replace ('HTTP : //', '', $ 변수)

+2

RE는 그렇게 단순한 것에는 너무 복잡합니다. – Zaffy

+1

나는 "이것은 시험되지 않은, .."비트로 downvoted. 해답을 게시하기 전에 솔루션이 실제로 작동하는지 확인하십시오. – NotMe