2012-06-26 35 views
0

我用PHP版Pinterest的引腳It按鈕的工作和我正在與該URL的「說明」部分的問題...PHP與網址友好的文本替換包括hashtag

我使用PHP生成的網址:

$pinurl = 'http://foo.com'; 
$pinmedia = 'http://foo.com/picture.jpg'; 
$pindesc = 'my description with #hashtag'; 

$pin = 'http://pinterest.com/pin/create/button/?url='.$pinurl.'&media='.$pinmedia.'&description='.$pindesc.''; 

echo '<a href="'.$pin.'" class="pin-it-button" count-layout="none" target="_blank">Pin It!</a>'; 

我碰上是當我的$ pindesc變量中有一個主題標籤或僅一個主題標籤,Pinterest的不從URL因爲它看起來像讀它的問題:

http://pinterest.com/pin/create/button/?url=http://foo.com&media=http://foo.com/foo.jpg&description=#foo 

顯然,#foo並沒有被當作描述來讀取......

我想知道是否有某種解決方法,或者如果我可以使用「if」語句來運行我的$ pindesc變量來檢查並用「%url」替換#標籤,這是「url友好型」

謝謝!

回答

3

您可以使用urlencode任何棘手的字符轉換在你的腳本:

$pindesc = urlencode('my description with #hashtag'); 
+0

正是我在找的!非常感謝! – tcd

1

使用rawurlencode

$pin = 'http://pinterest.com/pin/create/button/?url='.rawurlencode($pinurl).'&media='.rawurlencode$pinmedia).'&description='.rawurlencode($pindesc).''; 

這將URL編碼除了-_所有字符〜(RFC 3986)

+0

感謝這也! – tcd