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.
-(NS|PD|SP)-(\w+)inpreg_match_alluse both captured groups.preg_matchs. Just put the second one in the conditional.preg_matchtwice, build a pattern with capture groups to extract informations you want.if(preg_match_all('/-(NS|PD|SP)-(\w+)/', $path, $match)) { print_r($match); }working nicely. Thank you.