プラグイン使用時に<html>タグが削除される及び文字化けが発生する問題
現在公開されているEC-CUBE3向けのプラグインではプラグイン対象ページだと
<!doctype html> <html lang="ja"> </html>
のタグが出力されない現象及び文字化けが発生しています。
原因は、
$crawler->html();
を使うとタグが出力されない及び文字化けが発生します。
対処方法として、
public function Hoge(・・・・) { ・・・・ $crawler = new Crawler($response->getContent()); $html = $this->getHtml($crawler); ・・・ } /** * 解析用HTMLを取得 * * @param Crawler $crawler * @return string */ private function getHtml(Crawler $crawler) { $html = ''; foreach ($crawler as $domElement) { $domElement->ownerDocument->formatOutput = true; $html .= $domElement->ownerDocument->saveHTML(); } return html_entity_decode($html, ENT_NOQUOTES, 'UTF-8'); }
というようにgetHtmlを自作してdomElementを1行ずつ取得することで文字化けは直ります。
また、$crawler->filter
も同様に文字化けが発生しますので、
$oldElement = $crawler->filter('#main'); $oldHtml = $oldElement->html(); $oldHtml = html_entity_decode($oldHtml, ENT_NOQUOTES, 'UTF-8');
とすることでこちらも対応可能です。
本来であれば本体側で組み込んで欲しい機能ですね。