09/03/19 13:18:49
h1タグの部分を置換したいです。
もっと、スマートの方法ありますか?
(h1の要素は不特定、ない場合もあります。)
<?php
$file = "
<html><body>
<h1 id=\"news\" name=\"news\" style=\"size: medium;\">WBC速報</h1>
<p>4回裏 2 - 0</p>
</body></html>
";
preg_match_all('/<h1[^>]*?>(.*?)<\/h1>/is',$file , $match);
$tag = $match[0][0];
$title = $match[1][0];
$file = str_replace($tag,"<div>{$title}</div>", $file);
echo $file;
?>