JSONデコード後にHTMLエンティティのデコードを行う
JSONデコード前にHTMLエンティティのデコードを行ってしまうと " なども解除されてしまい、JSONとして不正な入力になる。 html_decode_entityのオプションで除外しても良いが、改行以外は一応JSONとして正当はなずなので、安全に倒してJSONとしてのデコードを済ませてから処理する。
This commit is contained in:
parent
2454a24ee2
commit
8772facadf
@ -36,12 +36,16 @@ class NijieResolver implements Resolver
|
|||||||
$metadata = $this->ogpResolver->parse($html);
|
$metadata = $this->ogpResolver->parse($html);
|
||||||
$crawler = new Crawler($html);
|
$crawler = new Crawler($html);
|
||||||
|
|
||||||
// DomCrawler内でjson内の日本語がHTMLエンティティに変換されるのでhtml_entity_decode
|
$json = $crawler->filter('script[type="application/ld+json"]')->first()->text();
|
||||||
$json = html_entity_decode($crawler->filter('script[type="application/ld+json"]')->first()->text());
|
|
||||||
|
|
||||||
// 改行がそのまま入っていることがあるのでデコード前にエスケープが必要
|
// 改行がそのまま入っていることがあるのでデコード前にエスケープが必要
|
||||||
$data = json_decode(preg_replace('/\r?\n/', '\n', $json), true);
|
$data = json_decode(preg_replace('/\r?\n/', '\n', $json), true);
|
||||||
|
|
||||||
|
// DomCrawler内でjson内の日本語がHTMLエンティティに変換されるので、全要素に対してhtml_entity_decode
|
||||||
|
array_walk_recursive($data, function (&$v) {
|
||||||
|
$v = html_entity_decode($v);
|
||||||
|
});
|
||||||
|
|
||||||
$metadata->title = $data['name'];
|
$metadata->title = $data['name'];
|
||||||
$metadata->description = '投稿者: ' . $data['author']['name'] . PHP_EOL . $data['description'];
|
$metadata->description = '投稿者: ' . $data['author']['name'] . PHP_EOL . $data['description'];
|
||||||
if (
|
if (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user