将编码的文本字符串包含在twitter分享按钮中的问题

时间:2013-08-20 09:18:46

标签: php wordpress twitter urlencode

所以我试图使用推特推文按钮来发布网页上的帖子。我直接使用Twitter的股票标准代码。

...假设

post_text =我们只有少数字符"在这里解释一下

<a class="twittersocial" onClick="_gaq.push(['_trackEvent', 'Twitter', 'Share', '<?php echo get_permalink($post->ID)?>']); window.open('https://twitter.com/share?url=<?php echo urlencode(get_permalink($post->ID))?>&text=<?php echo urlencode(substr(get_field('post_text'),0,86))?>...&via=twitterhandle&hashtags=newpost','Ratting','width=550,height=400,0,status=0,scrollbars=1');"href="javascript:void(0);" name="Share a quote on Twitter" title="Share a quote on Twitter" ></a>

结果是twitters弹出窗口:

我们&#039;只有&quot;个字符&quot;来解释这里的事情...... http://www.domain.com/newpost-x/ #newpost via @twitterhandle

确保帖子没有被HTML字符标记污染的最佳解决方案是什么?我正在努力使解决方案尽可能轻松,因为我正在替换一个非常繁重的社交媒体插件。

2 个答案:

答案 0 :(得分:1)

检查编码的URL字符串后,似乎get_field的'表示为&#039;,随后编码为%26%23039%3B(编码两次)

html_entity_decode用于将&#039;解码为',然后再将其编码回&#039;

substr(html_entity_decode(get_field('post_text'), ENT_QUOTES),0,156)

答案 1 :(得分:0)

strip_tags()怎么样?

substr(strip_tags(get_field('quote_text')), 0, 86)

这将在执行子字符串之前删除所有html标记。