【问题标题】:Stop DOMXPath evaluate from converting special characters停止 DOMXPath 评估转换特殊字符
【发布时间】:2019-02-06 20:40:28
【问题描述】:

如果您在带有特殊字符的 url 上运行此 DOMXPath 评估(我意识到在技术上不是有效的 url,但有些网站仍然有它们):

$a = '<a href="http://example.org/diseño"></a>';
$path = new DOMXPath( @DOMDocument::loadHTML( $a ) );
$href = $path->evaluate( 'string(//a/@href)' );

var_dump( $href );
// http://example.org/diseño

有没有办法获得一个带有“正确”href 的字符串——比如特殊字符?

http://example.org/diseño

或者 DOMXPath 是否真的可以正常工作,因为 url 不能有特殊字符?如果是这样,从包含特殊字符的字符串中获取 href 的最干净的替代方法是什么?

【问题讨论】:

  • 你可以试试echo utf8_decode($href);
  • 啊,这太棒了,请添加一个答案,我会接受它。

标签: php html


【解决方案1】:

您可以只对来自 XPath 的返回值进行编码...

echo utf8_decode($href);

这给了...

http://example.org/diseño

【讨论】:

    猜你喜欢
    • 2016-06-11
    • 2013-10-03
    • 1970-01-01
    • 2022-07-10
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 2019-11-04
    • 1970-01-01
    相关资源
    最近更新 更多