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.

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大