explode method:
You do not need to use complex regexp, you can use simple explode .
$parts = explode( '.', $string);
Details are now as 2 parts or 6, so you can do:
if( count( $parts) == 6)){ list( $fistName1, $surName1, $string, $fistName2, $surName2, $gameType) = $parts; } elseif( count( $parts) == 2) { $gameType = $parts[1]; list( $fistName1, $surName1, $string, $fistName2, $surName2) = explode( $parts[0]); } else { echo "Cannot parse"; }
And now parsing $gameType :)
if( preg_match( '~^\|(\d+)x(\d+)\|$~', $gameType, $parts)){ $first = $parts[1]; $second = $parts[2]; } elseif( preg_match( '~^G(\d+)R(\d+)$~', $gameType, $parts)){ $first = $parts[1]; $second = $parts[2]; } else { echo "Cannot parse!"; }
preg_match method:
The second regular expression is deliberately different, so you can see how to write a regular expression that will "eat" the whole name, it doesn't matter if it has 2,3 or 5 parts, and you get used to *? (greedy killer).
$match = array(); if( preg_match( '~^(\w+)\.(\w+)\.G(\d+)R(\d+)\.(\w+)\.(\w+)\.(\w+)$~', $text, $match)){ // First way } elseif (preg_match( '~^([^\|]+)\|(\d+)x(\d+)\|(.*?)\.(\w+)$~', $text, $match)){ // Second way } else { // Failed to parse }
Change (more than 2 names)
And if a player can have more than two names (e.g. Armin Van Buuren ), you should go with regexp as follows:
~^([\w.]+)\.G(\d+)R(\d+)\.([\w.]+)\.(\w+)$~
This will match the names in Albert.Einstein , Armin.Van.Buuren (regexp relies on this name, will not contain \d (decimal), so names like Gerold The 3rd do not match).
You should be fine using only: ~^([\w\d.]+)\.G(\d+)R(\d+)\.([\w\d.]+)\.(\w+)$~ , which will also match Gerold The 3rd , and any other name ( \.G(\d+)R(\d+)\. is pretty strict, and you will need to make a really crazy name like G3R01 (like " 3l1t33 guy Herald ") to parse it wrong.
Oh and one more thing, don't forget $name = strtr( $name, '.', ' ') :)
explained by RegExp
~~ - regexp delimiter ; begin end ends regexp; ~regexp~ , it can be almost everything /regexp/ , (regexp)^ and $ are metacharacters ; ^ start of line / line, $ end of line / line\w escape sequence for any character in a word, like [a-zA-Z]([\w.]+) - commits the subpatern / match group that contains [a-zA-Z.] at least once. + called quantifier+? - ? (after another quantifier) ββis called the greedy killer, and this means as little as possible, usually (\w+)a will match (on the ababa line) abab , (\w+?)a will match ab and (\w*?)a will match empty line :)