1

I've been trying to figure this out all day but how can I reliable split the next line in following order.

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 (+). Evolved Enlarged Claws: 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.

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.
        )
)

I can't get past the mid sentence periods.

Johan
  • 25
  • 3
  • 2
    You're better off looking for the keywords that have a colon at the end ("Active:", "Passive:" and "Evolved Enlarged Claws:") and using those. I can't see any way of splitting this up using periods. – andrewsi Sep 27 '12 at 15:58
  • It'll be matching `Passive`, `Active`.. in that order? – hjpotter92 Sep 27 '12 at 15:58
  • http://stackoverflow.com/questions/5032210/php-sentence-boundaries-detection – Ja͢ck Sep 27 '12 at 15:59
  • 3
    Well where is your regex then? You want us to correct something you don't show... – arkascha Sep 27 '12 at 15:59
  • @andrewsi Sorry let me clarify I didn't want to split up on the periods. I was trying to split it up on the colon and select the first part by going back to the previous period. But I can't figure out how to express this in regex. – Johan Sep 27 '12 at 16:01
  • You might want to add your regex to the question, in that case. – andrewsi Sep 27 '12 at 16:04

3 Answers3

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

Name is in capture group 1, description is in capture group 2

Patrick
  • 1,766
  • 1
  • 15
  • 27
1

Well, this seems to do the trick...

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

... if I understood the structure given ('Term: Description. Term: Description', etc). It's actually colons within Description that might break it; dots do just fine here.

The result of this match can be quite easily transformed into an associative array:

$spell = array();
foreach ($matches as $match) {
   $spell[$match[1]] = $match[2];
}
raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • This still seemed to error on the last part of the line. Patrick's solution however did work. – Johan Sep 27 '12 at 21:06
0

Use . as delimiter for parent/main array and : for sub arrays.

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

    return $result;
}

Use:

 $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>';
kaiser
  • 21,817
  • 17
  • 90
  • 110
  • 1
    Did you check it? Apart from mistake (no argument passed to function, it should be written as `function someName ($your_string)`, perhaps), it breaks the same way as the OP mentioned. – raina77ow Sep 27 '12 at 16:15
  • @raina77ow No, I didn't - currently ain't in range of my dev env. Fixed. Thanks. – kaiser Sep 27 '12 at 16:23