Reputation: 43
I want to extract two values from some URLs.
Example URLs:
cricket-ck-game-play
hand-ball-hb-game-play
volley-ball-vb-game-play
soccer-sc-game-play
I want extract the full game name and its short name separately ignoring the -game-play
part at the end of the URL.
I've tried the following code which returns 'ck', 'hb', 'vb', 'sc', but not the full name.
preg_match("/(?<=-)[^-]*(?=-game-play)/", $uri, $game);
Actual results I am getting:
array(1) {
[0]=>
string(2) "ck"
}
Expected Results:
case:1 (In case of example 1) - array(2) {
[0]=>
string(7) "cricket"
[1]=>
string(2) "ck"
}
case:2 (In case of example 2) - array(2) {
[0]=>
string(9) "hand-ball"
[1]=>
string(2) "hb"
}
Upvotes: 2
Views: 1426
Reputation: 163362
If the parts before the short name are always more than 3 chars and the short name always consists of 2 chars [a-zA-Z]
you could use 2 capturing groups, using quantifiers {3,}
and {2}
to match the characters.
\b(\w{3,}(?:-(?:\w{3,}))*)-(\w{2})-game-play\b
Explanation
\b
Word boundary(
Capture group 1
\w{3,}
Match 3+ times a word char(?:-\w{3,})*
Repeat 0+ times matching -
and 3 or more word chars )
close group(
Capture group 2
[a-zA-Z]{2}
Match 2 times a word char (As per the comments))
Close group-game-play
Match literally\b
Word boundaryAnother option is to use preg_split and split on 2 chars a-z surrounded by hyphens in a capturing group and match the rest:
$pattern = "/-([a-z]{2})-.*/";
$str = "cricket-ck-game-play";
print_r(preg_split($pattern, $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
Result
Array
(
[0] => cricket
[1] => ck
)
Upvotes: 1
Reputation: 27723
Maybe, this expression might be somewhat close to what you have in mind:
(.*)-(.{2})(?=-game-play)
or
(.*)-(.*)(?=-game-play)
Upvotes: 1
Reputation: 654
preg_match("/(.+)-([a-z]{2})-/i", $uri, $game);
Grabs everything before two letters bordered by dashes, and then grabs those two letters.
Upvotes: 3