3

I have several urls such as below and those contains -XX- letter and following xxxxxxxxx key in the end of the url.

http://vayes-eys.dev/shoe-for-ladies/high-hields/7-pont-with-silver-stripes-PD-0a8564q56

or

http://vayes-eys.dev/news/europe/england/cricket-news/josh-darpant-is-on-the-way-to-rome-NS-e3q3s2wq4q

What I want to do is; first to check if -NS-, -PD- or -SP- exist in url, then get the -XX- part and the part after it,for example: e3q3s2wq4q.

What I have done so far is:

$path = "shoe-for-ladies/high-hields/7-pont-with-silver-stripes-PD-0a8564q56"

if (preg_match('/-PD-|-NS-|-SP-/',$path)) {
    preg_match("/(?<=(-PD-|-NS-|-SP-)).*/", $path, $match);
    print_r($match);
}

This gives me the following array but I am not sure if it is the right way.

array(
    0 => 0a8564q56
    1 => -PD-
)

What I need is PD and 0a8564q56. Thanks for any help.

4
  • 1
    Use -(NS|PD|SP)-(\w+) in preg_match_all use both captured groups. Commented Sep 6, 2016 at 14:49
  • Why the 2 preg_matchs. Just put the second one in the conditional. Commented Sep 6, 2016 at 14:51
  • There's no need to use preg_match twice, build a pattern with capture groups to extract informations you want. Commented Sep 6, 2016 at 14:51
  • 1
    @anubhava if(preg_match_all('/-(NS|PD|SP)-(\w+)/', $path, $match)) { print_r($match); } working nicely. Thank you. Commented Sep 6, 2016 at 15:08

1 Answer 1

2

You may use

'~-(NS|PD|SP)-([^-/]+)~'

or

'~-(NS|PD|SP)-([A-Za-z0-9]+)~'

See the regex demo

Details:

  • - - a literal hyphen
  • (NS|PD|SP) - Group 1 capturing one of the values: NS, PD or SP
  • - - a hyphen
  • ([^-/]+) - 1 or more characters other than - and / (the delimiters). If you only have letters and digits there, you may just use [a-zA-Z0-9]+ instead.

PHP demo:

$path = "shoe-for-ladies/high-hields/7-pont-with-silver-stripes-PD-0a8564q56";
preg_match('~-(NS|PD|SP)-([A-Za-z0-9]+)~', $path, $match);
print_r($match);

Your values are in Group 1 and 2.

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

5 Comments

Stribizew, I works nicely, Thank you. Two question: One of the friends here suggested (\w+) for the second group. What's difference with your ([^-/]+)..
Maybe one more, if you have time. Can we get also the part before -NS- at the same preg_match?
Is that part optional? What should it look like if you have the input as in my code snippet? 7-pont-with-silver-stripes or stripes?
@YahyaE You can use the regex101 provided link to alter the regex and see how it functions with the alterations. Here's an update with the \w+ regex101.com/r/vC2fI1/2. The site also provides a description of each part on the right side. \w+ is any alpha character, number, or under score. [^-/]+ is any non - or -.
@WiktorStribiżew That part is not optional. http://abc.ltd/<group1>-<NS|PD>-<group3> or http://abc.ltd/sth/smw/smo/<last-segment>-<NS|PD>-<group3>. I can use both.

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.