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


Issue with including encoded text string into twitter sharing button

所以我正试图使用twitter推特按钮从网页推特帖子。我使用的是直接来自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>

推特弹出窗口中的结果:

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

确保帖子不被HTML字符标记污染的最佳解决方案是什么?我希望让这个解决方案尽可能轻,同时我正在替换一个非常重的社交媒体插件。

溶液

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

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

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

strip_tags()怎么样?

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

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