Okay, lets say I have the following 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>
<h4><a name="test5"></a>Test 5</h4>test 5 text
I would like to use regular expressions to replace that text with the following:
<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&开发者_开发百科gt;
<div class="toggle">
<h4><a name="test1"></a>Test 5</h4>
<div class="contents">test 5 text</div>
</div>
I tried using:
$text = preg_replace('#<h(\d+)>(.*?)</h\1>(.*)#si', '<div class="ToggleContents"><h$1>$2</h$1><div class="contents">$3</div></div>', $text);
But the problem with this is that it's far too greedy and I haven't figured out how to make it ungreedy and work. Anyone have any ideas?
$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);
Or the <span>
solution:
$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);
I figured it out... I just appended ##### to all headers, and seeked to that.
$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']);
Split it into three different search and replace operations?
精彩评论