kiss-o-matic Posted June 18, 2006 Share Posted June 18, 2006 Can't figure this one out.[code]if ( preg_match("/(flag1.+)*(flag2.+)*/s", $definition, $matches ) ) { echo "found " . sizeof($matches) . " matches<br>"; for ( $i = 1; $i <= sizeof($matches); $i++ ) { echo "$i :" . $matches[$i] . "<br>"; }} else { echo "no match<br>";}[/code]$definition will most of the time look like this:[code]flag1He runsShe runsThey runflag2he criesshe criesthey cry[/code]Although sometimes it will have text only in the flag1 section, and sometimes only in the flag2 section. So naturally, I want to match zero or more occurences of both of them. I thought the above would work, but alas, it does not.. In fact, it's only reporting one match. O_O Seems if I use one *, I can half of it to work, but of course, I need all of it to. Link to comment https://forums.phpfreaks.com/topic/12286-trying-to-match-zero-or-more-occurences/ Share on other sites More sharing options...
effigy Posted June 19, 2006 Share Posted June 19, 2006 [code]<?php$definition = <<<DEFflag1He runsShe runsThey runflag2he criesshe criesthey cryDEF; $initial_data = preg_split( '/(flag.+)/', $definition, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); echo '<pre>', print_r($initial_data, true), '</pre>'; $key = ''; $index = 0; foreach ($initial_data as $item) { $item = trim($item); ### Values if (!($index % 2)) { $key = $item; } ### Keys else { $final_data["$key"] = explode("\n", $item); } ++$index; } echo '<pre>', print_r($final_data, true), '</pre>';?>[/code] Link to comment https://forums.phpfreaks.com/topic/12286-trying-to-match-zero-or-more-occurences/#findComment-47332 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.