2012-09-27 61 views
2

我正在使用preg_match_all(),我的問題是我無法創建我想要的模式。源文本的例子:創建模式爲php preg_match_all

<td align='left'> 
    <span style='font-size: 13px; font-family: Verdana;'><span> 
</td> 
<td> 
    <a style='color: #ffff00' rel='gb_page_fs[]' title='Parodyk kitiems 8 seriją' href='/pasidalink-19577x10/'> 
     <img src="/templates/filmai_black/images/ico_tool_share.gif" /> 
    </a> 
</td> 
<td> 
    <small>LT titrai</small> 
</td> 
<td> 
    <a rel='gb_page_center[528, 290]' title='Žiūrėti 8 seriją' href='http://www.filmai.in/watch.php?em=BuwgzpqtssiAGGcjeekz9PTI1NjQ0N2E~'> 
     <img src="/templates/filmai_black/images/play_icon.png" width="20" onclick='set_watched_cookie_serial("19577x10", "done-tick-full-series")' /> 
    </a> 
</td> 

我使用的模式:

<td><small>(.*)</small></td> 
<td><a rel='gb_page_center[528, 290]' title='Žiūrėti (.*) seriją' href='(.*)'><img src= 

我想要得到的位置的內容到一個數組(*)。

有人可以糾正我的模式並解釋它嗎? 我想學習使用正則表達式。

+4

你不應該對'html'使用正則表達式,而應該使用'DOMDocument'。 –

+1

是的,如果涉及HTML提取,沒有經驗的編碼人員會使用正則表達式語法。有[更簡單的選項](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php)。 – mario

回答

0

不要使用正則表達式來解析HTML」放在一邊, 這裏有幾個簡單的步驟來學習正則表達式。

  1. Download and install RegexBuddy
  2. 運行使用RegexBuddy
  3. Start with something easy再飛! :)

你正在尋找的表達式爲:

<small>(.*)</small> 

它發現其間的發現標籤small所有字符,並將它們放入反向引用。 將Backreference想象爲一個數組。找到的第一個項目是0,接下來是1,依此類推。

// command: 
preg_match_all('%<small>(.*)</small>%i', $subject, $result, PREG_PATTERN_ORDER); 

// $result[0] 

Array 
(
    [0] => <small>LT titrai</small> 
)