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.

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

报告相同问题?