2012-09-27 64 views
1

我一直試圖弄清楚這一整天,但我怎麼可以可靠地拆分下一行的順序。我試圖用正則表達式分割一個句子

被動:標記與附近盟友隔離的敵人。主動: 特賣70/100/130/160/190(+)物理傷害。如果目標是孤立的 ,則金額將增至100/145/190/235/280(+)。進化 擴大爪子:對其孤獨的敵人造成的傷害提高012/16/12/12/12/12%12/12/12/12/12%(最大200/200/200/200/2001與 怪物)。使他們的恐懼品味和Kha'Zix的基本攻擊範圍增加50/50/50/50/50。

Array 
(
    [0] => Array 
     (
      [0] => Passive 
      [1] => Marks enemies who are isolated from nearby allies. 
     ) 
    [1] => Array 
     (
      [0] => Active 
      [1] => Deals 70/100/130/160/190 (+) physical damage. If the target is isolated, the amount is increased to 100/145/190/235/280 (+). 
     ) 
    [2] => Array 
     (
      [0] => Evolved Enlarged Claws 
      [1] => Increases damage to isolated enemies by 12/12/12/12/12% of their missing health (max 200/200/200/200/200 vs. monsters). Increases the range of Taste Their Fear and Kha'Zix's basic attacks by 50/50/50/50/50. 
     ) 
) 

我不能讓過去的中期句段。

+2

你最好尋找一個在有一個冒號的關鍵字結束(「主動:」,「被動:」和「進化的爪子:」)並使用它們。我看不出有什麼辦法可以用句點來分解它。 – andrewsi

+0

它將按照「被動」,「主動」的順序匹配? – hjpotter92

+0

http://stackoverflow.com/questions/5032210/php-sentence-boundaries-detection –

回答

2
(?:^|\s*)([^.:]+):\s*(.+?\.)(?=[\w\s]+:|$) 

名稱是捕獲組1,說明在捕獲組2

+0

非常感謝,這正是我想要的! – Johan

1

嗯,這似乎這樣的伎倆......

preg_match_all('/(?<=^|[.])([^:]+): (.+?[.])(?=[^.]+:|$)/', $text, $matches, PREG_SET_ORDER); 
var_dump($matches); 

...如果我的理解('Term: Description. Term: Description'等)給出的結構。它實際上是描述中的冒號,可能會破壞它;點在這裏做得很好。

此匹配的結果可以很容易地轉變成一個關聯數組:

$spell = array(); 
foreach ($matches as $match) { 
    $spell[$match[1]] = $match[2]; 
} 
+0

這似乎仍然在線的最後部分錯誤。但帕特里克的解決方案確實奏效 – Johan

0

.使用作爲分隔符的父/主陣列和:爲子陣列。

function so12625378_explode_to_multiarray($string) 
{ 
    foreach (explode(". ", $your_string) as $part) 
     $result[] = explode(": ", $part); 

    return $result; 
} 

用途:

$string = 'Passive: Marks enemies who are isolated from nearby allies. Active: Deals 70/100/130/160/190 (+) physical damage. If the target is isolated, the amount is increased to 100/145/190/235/280 (+).'; 
echo '<pre>'.var_export(so12625378_explode_to_multiarray($string), true).'</pre>'; 
+1

你檢查了嗎?除了錯誤(沒有參數傳遞給函數,它應該寫成'function someName($ your_string)',或許),它和OP提到的一樣。 – raina77ow

+0

@ raina77ow不,我沒有 - 目前不在我的開發環境範圍內。固定。謝謝。 – kaiser