2012-06-26 36 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