dsvyc66464 2017-02-09 09:21
浏览 50
已采纳

PHP将字符串编码为HTML

I have this code below, which comes from Wordpress Core file /wp-admin/includes/class-wp-posts-list-table.php

  printf(
    '<a class="row-title" href="%s" aria-label="%s">%s%s</a>',
    get_edit_post_link( $post->ID ),
    /* translators: %s: post title */
    esc_attr( sprintf( __( '&#8220;%s&#8221; (Edit)' ), $title )),
    $pad,
    $title
  );

In my case the variable $title contains iconfont HTML i.e.

  <i class="fa fa-heart"></i> 

enter image description here

The PHP code is making the web browser display the HTML characters as a string rather than what I'm after which is to display as HTML and render the Font Awesome Icon.

Ive tried wrapping $title in

htmlentities()
html_entity_decode()
htmlspecialchars()

Can some one help thanx

  • 写回答

1条回答 默认 最新

  • dongxu7121 2017-02-09 10:00
    关注

    Where did you use html_entity_decode? I've tried this:

    printf(
        '<a class="row-title" href="%s" aria-label="%s">%s%s</a>',
        get_edit_post_link( $post->ID ),
        /* translators: %s: post title */
        esc_attr( sprintf( __( '&#8220;%s&#8221; (Edit)' ), $title )),
        $pad,
        html_entity_decode($title)
      );
    

    and seems it works.

    But this is BAD idea to change core file. You can try to write (or find) some plugin that allows to add icons to particular post title but not to all post and without changing original file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题