我有一些字符串:如何提取第一個(可變)html標記之間的部分字符串?
$string1 = '<p><strong>Extract me</strong></p><p>Leave me</p>';
$string2 = '<strong>Extract me</strong>Leave me';
$string3 = '<span style="font-weight: bold">Extract me</span><br /><span>Leave me</span>';
讓我們來看看$ STRING3:
字符串的第一個標籤是<span>
所以第一<span>
和第一</span>
之間的文本要成爲提取。
提取是指:從$字符串X將其刪除,並保存到$ extractedX
我將如何做到這一點?
我用正則表達式嘗試了很多東西,但失敗了。 我對寫這篇維基百科文章的人感到羞恥。 – iceteea 2012-04-27 07:03:00
一般來說,我會反對使用正則表達式來解析html/xml結構。有更好的方法(例如[Dom *](http://php.net/manual/book.dom.php),[SimpleXml](http://php.net/manual/book.simplexml.php))。 – Yoshi 2012-04-27 07:04:33
聽起來似乎合理。 但我不知道如何解決我的問題一般。 – iceteea 2012-04-27 07:06:27