2

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"  
}
2
  • 2
    Are the parts before ck etc always more than 2 chars and are the short names always 2 chars? Are they always characters a-z? Commented Aug 22, 2019 at 18:55
  • 1
    Yes the parts before 'ck' are always more than 2 chars and also the short names are always 2 chars and are always alphabets only(can be capitals sometimes or Uc sort of) Commented Aug 22, 2019 at 19:15

3 Answers 3

3
preg_match("/(.+)-([a-z]{2})-/i", $uri, $game);

Grabs everything before two letters bordered by dashes, and then grabs those two letters.

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe, this expression might be somewhat close to what you have in mind:

(.*)-(.{2})(?=-game-play)

Demo 1

or

(.*)-(.*)(?=-game-play)

Demo 2

3 Comments

Does not result in desired output. Game name can have a hyphen in it.
@Emma Thanks! Your condition 1 worked like a charm!
These work, but you don't need the lookahead. (.*)-(.*)-game-play is very elegant.
1

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 boundary

Regex demo | Php demo

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

Php demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.