當出現特定變量時,我需要將我的html
分成兩部分。拆分時動態重建html標記
下面是一個例子:
<h1>I can have any kind of text here</h1>
<p> </p>
<p><em><strong>When I see this variable :)</strong> I want to rebuild my html.</em></p>
我想在最後什麼:
<h1>I can have any kind of text here</h1>
<p> </p>
<p><em><strong>When I see this</strong></em></p>
<p><em><strong> :)</strong> I want to rebuild my html.</em></p>
這是我想(告訴我,如果有更好的方法):
- 在我的變量上分割
- 通過正則表達式獲取所有標記? (我只是知道正則表達式來告訴我是真是假)
- 把所有的標籤都放在堆棧上?
- 循環兩次(陣列分割1和陣列分開2)
EDIT實施例2:
<ol>
<li>
<h2>I can have any kind of text here 1</h2>
</li>
<li>
<h2><strong>I can have any kind of text here 2 variable</strong></h2>
</li>
<li>
<h2><strong>I can have any kind of text here 3</strong></h2>
</li>
<li>
<h2>I can have any kind of text here 4</h2>
</li>
<li><em><strong>When I see this</strong></em></li>
</ol>
<p> </p>
...
<ol>
<li>
<h2>I can have any kind of text here 1</h2>
</li>
<li>
<h2><strong>I can have any kind of text here 2 variable </strong></h2>
</li>
</ol>
可變
<ol>
<li>
<h2><strong>I can have any kind of text here 3</strong></h2>
</li>
<li>
<h2>I can have any kind of text here 4</h2>
</li>
<li><em><strong>When I see this</strong></em></li>
</ol>
<p> </p>
爲例3:
<p><a href="test">I can have any kind of text of varaible here even clickable.</a></p>
...
<p><a href="test">I can have any kind of text of</a></p>
varaible
<p><a href="test">here even clickable.</a></p>
第5步:忘記第1步 - 4並改用解析器? – Jan
的確有很多html解析器。 –