【问题标题】:PHP URL title looking funnyPHP URL 标题看起来很有趣
【发布时间】:2017-09-28 10:31:32
【问题描述】:

下面的代码给了我一个带有“%20”而不是空格的链接标题,并在标题前添加了“http://”。

“user_url”是链接地址。

“标语”是标题文本(例如“Lorem ipsum dolor sit amet, consectetur adipiscing elit...”)

输出为:“http://Lorem&20ipsum&20dolor&20sit&20amet&20consectetur&20adipiscing&20elit

我想要的只是干净的文本。

有什么建议吗?

代码如下:

<?php if( !empty( $current_author_profile->user_url ) ) {?>
<li><i class="fa fa-link"></i><a href="<?php echo esc_url( $current_author_profile->user_url );?>" target="_blank" title="<?php echo esc_url( $current_author_profile->tagline );?>"><?php echo docdirect_parse_url( $current_author_profile->user_url);?></a></li>
<?php }?>

谢谢! :)

【问题讨论】:

  • 明文是什么意思
  • 当我悬停链接时,我想要“Lorem ipsum dolor sit amet, consectetur adipiscing elit”而不是“http;//Lorem&20ipsum&20dolor&20sit&20amet&20consectetur&20adipiscing&20elit”
  • 但我认为这个链接应该是连字符

标签: php


【解决方案1】:

最好使用htmlentities(),因为标题需要在双引号之间进行转义,因为如果文本本身包含双引号,它会破坏标题甚至破坏html标签的功能。见http://php.net/manual/en/function.htmlentities.php

htmlentities — 将所有适用的字符转换为 HTML 实体

title="<?php echo htmlentities($current_author_profile->tagline, ENT_QUOTES | ENT_HTML5);?>"

我更喜欢在htmlentities() 函数的第二个参数flags 中用ENT_QUOTES 引用双引号和单引号。有关可用标志、编码参数等的更多详细信息,请查看文档链接。

【讨论】:

  • 这超出了我的知识范围,但代码有效,我会相信你所说的其余部分。谢谢!
【解决方案2】:

那是因为你正在对你的标题进行 URL 转义:

title="<?php echo  $current_author_profile->tagline;?>"

应该可以。

【讨论】:

  • 就像一个魅力 :) 谢谢!
猜你喜欢
  • 2013-06-24
  • 2012-05-01
  • 2018-01-17
  • 1970-01-01
  • 2012-12-27
  • 2013-05-13
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
相关资源
最近更新 更多