簡體   English   中英

PHP如何將字符串從DomCrawler轉換為UTF-8

[英]PHP How to convert strings from DomCrawler to UTF-8

我有一些我用DomCrawler收集的數據並存儲在一個數組中,但看起來他在è,à,ï等特殊字符時失敗了。

作為一個例子,當我回應結果時,我得到è而不是è

當我將結果存儲在\Ã\¨文件中時,我得到了: \Ã\¨我的目標是將特殊字符保存在\Ã\¨文件中。

我試過編碼它但似乎沒有我想要的結果。

$html = file_get_contents($url);
$crawler = new Crawler($html);

$h1 = $crawler->filter('h1');
$title = $h1->text();
$title = mb_convert_encoding($title, "HTML-ENTITIES", "UTF-8");

無論如何我可以展示我的特殊角色嗎?

非常感謝!

通過使用構造函數添加HTML,爬網程序假定它在ISO-8859-1中。 您必須使用addHTMLContent方法明確告訴它您的DOM是UTF-8:

$html = file_get_contents($url);
$crawler = new Crawler;
$crawler->addHTMLContent($html, 'UTF-8');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM