遇到PHP preg_replace问题

时间:2011-08-25 01:21:09

标签: php preg-replace

好的,我可以说我有以下文字......

<h2><a name="test1"></a>Test 1</h2>test 1 text
<h2><a name="test2"></a>Test 2</h2>
test 2 text
<h3><a name="test3"></a>Test 3</h3>
test
3
text
<h4><a name="test4"></a>Test 4</h4>
<h4><a name="test5"></a>Test 5</h4>test 5 text

我想使用正则表达式用以下内容替换该文本:

<div class="toggle">
    <h2><a name="test1"></a>Test 1</h2>
    <div class="contents">test 1 text</div>
</div>
<div class="toggle">
    <h2><a name="test1"></a>Test 2</h2>
    <div class="contents">test 2 text</div>
</div>
<div class="toggle">
    <h3><a name="test1"></a>Test 3</h3>
    <div class="contents">test
3
text</div>
</div>
<div class="toggle">
    <h4><a name="test1"></a>Test 4</h4>
    <div class="contents"></div>
</div>
<div class="toggle">
    <h4><a name="test1"></a>Test 5</h4>
    <div class="contents">test 5 text</div>
</div>

我尝试使用:

$text = preg_replace('#<h(\d+)>(.*?)</h\1>(.*)#si', '<div class="ToggleContents"><h$1>$2</h$1><div class="contents">$3</div></div>', $text);

但问题在于它太贪婪了,我还没弄明白如何让它变得不合适并且有效。有人有什么想法吗?

3 个答案:

答案 0 :(得分:3)

$text = '<h2><a name="test1"></a>Test 1</h2> test 1 text
<h2><a name="test2"></a>Test 2</h2> test 2 text
<h3><a name="test3"></a>Test 3</h3> test 3 text
<h4><a name="test4"></a>Test 4</h4> test 4 text
<h4><a name="test5"></a>Test 5</h4> test 5 text';

$text = preg_replace('#<h([1|2|3|4|5|6])>(.*?)</h([1|2|3|4|5|6])>\ (.*?)(\n|$)#si', '
<div class="toggle">
  <h\\1>\\2</h\\1>
  <div class="contents">\\4</div>
</div>
', $text);

<span>解决方案:

$text = '<h2><a name="test1"></a>Test 1</h2><span>test 1 text</span>
<h2><a name="test2"></a>Test 2</h2><span>test 2 text</span>
<h3><a name="test3"></a>Test 3</h3><span>test 3 text</span>
<h4><a name="test4"></a>Test 4</h4><span>test 4 text</span>
<h4><a name="test5"></a>Test 5</h4><span>test 5 text</span>';

$text = preg_replace('#<h([1|2|3|4|5|6])>(.*?)</h([1|2|3|4|5|6])><span>(.*?)</span>#si', '
<div class="toggle">
  <h\\1>\\2</h\\1>
  <div class="contents">\\4</div>
</div>
', $text);

答案 1 :(得分:3)

我想通了......我只是将#####附加到所有标题中,并寻求这样做。

$page['HTML'] = preg_replace('#(<h(\d+)>)#i', '#####$1', $page['HTML']);
$page['HTML'] = preg_replace(
        '#<h(\d+)>(.*?)</h\1>(.*?)(\#{5}|$)#si',
        '<div class="ToggleContents"><h$1>$2</h$1><div class="contents">$3</div></div>',
        $page['HTML']);
$page['HTML'] = preg_replace(
        '#<div class="ToggleContents"><h(\d+)>(.*?)</h\1><div class="contents">([<br\s/>]*?)</div></div>#i',
        '<h$1>$2</h$1>$3',
        $page['HTML']);
$page['HTML'] = str_ireplace('#####', '', $page['HTML']);

答案 2 :(得分:1)

将其拆分为三种不同的搜索和替换操作?