我需要找到一种方法来替换之前的所有内部。
下面是一个示例html:
<p>2012/01/03</p>
<blockquote>
    <h4>File name</h4>
    <p>Good Game</p>
</blockquote>
<blockquote><p>Laurie Ipsumam</p></blockquote>
<h4>Some title</h4>
<hr />
<p>Lorem Ipsum</p>
<blockquote><p>Laurel Ipsucandescent</p></blockquote>下面是我得到的信息:
    $pieces = explode("<hr", $theHTML, 2);
    $blocks = preg_match_all('/<blockquote>(.*?)<\/blockquote>/s', $pieces[0], $blockmatch); 
    if ($blocks) { 
        $t1=$blockmatch[1];
        for ($j=0;$j<$blocks;$j++) {
            $paragraphs = preg_match_all('/<p>/', $t1[$j], $paragraphmatch);
            if ($paragraphs) {
                $t2=$paragraphmatch[0]; 
                for ($k=0;$k<$paragraphs;$k++) { 
                    $t1[$j]=str_replace($t2[$k],'<p class=\"whatever\">',$t1[$j]);
                }
            }
        } 
    } 我想我真的很接近了,但是我不知道如何把我刚刚拼凑和修改的html重新组合在一起。
发布于 2012-01-04 07:35:40
$string = explode('<hr', $string);
$string[0] = preg_replace('/<blockquote>(.*)<p>(.*)<\/p>(.*)<\/blockquote>/sU', '<blockquote>\1<p class="whatever">\2</p>\3</blockquote>', $string[0]);
$string = $string[0] . '<hr' . $string[1];输出:
<p>2012/01/03</p>
<blockquote>
    <h4>File name</h4>
    <p class="whatever">Good Game</p>
</blockquote>
<blockquote><p class="whatever">Laurie Ipsumam</p></blockquote>
<h4>Some title</h4>
<hr />
<p>Lorem Ipsum</p>
<blockquote><p>Laurel Ipsucandescent</p></blockquote>https://stackoverflow.com/questions/8719315
复制相似问题