dongtu1357 2017-01-23 02:28
浏览 55

我的php函数做了我想要的但是输出将字符转换为ascii

I am using the following function (tried both in my Wordpress Child Theme function file as well as a plugin - works in both cases) to remove hashtags from post titles. The function does what I want but then all titles (post body content is ok just titles) are now showing HTML numbers for characters (i.e., ' = &8217; and & = &038; and - = &8211;).

So this title

That's #testing the & and apostrophe #tagstitle #cats #cat #instagramcats

becomes

That&8217;s testing the &038; and apostrophe

Which removes hashtags as desired but creates the character issue.

function remove_hashtags($string){
    return preg_replace('/#(?=[\w-]+)/', '', 
    preg_replace('/(?:#[\w-]+\s*)+$/', '', $string));
}
add_filter('the_title', 'remove_hashtags');

I've tried adding additional code:

html_entity_decode('the_title', ENT_QUOTES | ENT_XML1, 'UTF-8');

to the function after reading up on PHP (I'm just learning) but it doesn't seem to work and I'm not sure how to use

html_entities($string)

(question update adding more information)

I basically took the code from here - that had exactly what I needed. I just added the last lines for filtering the WordPress Post Titles.

I don't want to make it too complicated a question but ideally I would like to remove the hashtags from the text and actually create post tags from them. I have found several answers for each part I just don't know how to put it all together. Forget that though...I really just want to find out why all of the sudden the ascii numbers are replacing the original punctuation.

  • 写回答

1条回答 默认 最新

  • dongxi8297 2017-01-23 04:25
    关注

    If you only want to remove hashtags you could use str_replace("#","",$string);

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?