简体   繁体   中英

Need PHP regex to match pattern in URL

I have problem with matching pattern in URL and getting the right result.

The URL example:

#1: http://www.example.com/webapp/first-text-vs-some-text.html
#2: http://www.example.com/webapp/first-text-a1-vs-sec-b2-text.html
#3: http://www.example.com/webapp/first-text-a1-vs-sec-b2-text-vs-third-c3-text.html
#4: http://www.example.com/webapp/some-text-a1-vs-sec-text-b2-vs-third-text-vs-last-text-c1.html

Result without -vs- (HINT: -vs- can be shown once or repetitive from above URLs):

#1: first-text and some-text
#2: first-text-a1 and sec-b2-text
#3: first-text-a1, sec-b2-text and third-c3-text
#4: some-text-1a, sec-text-b2, third-text and last-text-c1

Is there a way of PHP loop to do that?

There is a situation that -vs- in URL structure can be found once or multiple times.

Should I get my results with few patterns or can it be possible done with one pattern match?

Should I first split the given URL and then check for characters & symbols left or right of the -vs-?

Had You have that kind of situation ever? Any ideas?

Thanks a lot for help and sharing ideas to get final result!

You can use the following regex ([^\\/.]+?)(?:-vs-|\\.\\w+$) with preg_match_all to return the different parts before and after -vs- .

Regex demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM