我试图从一个超文本标记语言字符串中提取所有的链接文本和href,但是源字符串是Unicode,而nodeValue似乎不能处理这个问题?
$links = array();
$titles = array();
$dom = new DOMDocument();
$dom->loadHTML( $str );
$hrefs = $dom->getElementsByTagName("a");
foreach ($hrefs as $href) {
$links[] = $href->getAttribute("href");
$titles[] = $href->nodeValue;
}我的源字符串如下所示:
<p><a href='uploads/root/tr_62.pdf'>Türkiye</a></p> 但我对$titles的输出如下所示:
Türkiye如何让nodeValue尊重Unicode字符?
感谢您的关注!
发布于 2017-02-15 19:30:25
您经常使用mb_convert_encoding吗
$dom = new DOMDocument();
$html_data = mb_convert_encoding($str , 'HTML-ENTITIES', 'UTF-8');
$dom->loadHTML( $html_data );
$hrefs = $dom->getElementsByTagName("a");
foreach ($hrefs as $href) {
$links[] = $href->getAttribute("href");
$titles[] = $href->nodeValue;
}发布于 2017-02-15 19:37:55
谢谢,用户Veve的评论回答了我的问题。
下面这行代码解决了我的问题:
$str = mb_convert_encoding( $str, 'html-entities', 'utf-8' ); https://stackoverflow.com/questions/42248014
复制相似问题