php regex match word and stop - php

I have a URL like this http://website.com/clothes/men/type/t-shirts/color/red/size/xl... and I need to perform an action when the url is like this http://website.com/clothes/(men or woman)/type/any-type
So if the after type/any-type there are other values I don't want to perform the action.
My regex looks like this right now preg_match('/clothes\/(men|women)\/type\/(.*)\/?$/', $_SERVER['REQUEST_URI'])
It matches the case I want, but it also matches if the URL continues after that specific key/value pair, so it also matches http://website.com/clothes/men/type/t-shirts/color/red.
So in the end I need the preg_match() to only match a URL that has only a type/anything pair.
Thank you.

You can use:
if ( preg_match('~/clothes/(?:wo)?men/type/[^/]+/$~i', $_SERVER['REQUEST_URI'], $m) ) {
// matches succeeds
}
You can use alternate delimiter like ~ to avoid escaping every forward slash
Remove .* in the end if you don't want to match after .../type/any-type/

You can just match [^/]+:
preg_match('(clothes/(men|women)/type/([^/]+)/?$)', $_SERVER['REQUEST_URI'])

Related

PHP Regex to match url contains url fragment

I have one url fragment: page/login and i need to know if another url fragment contains them.
These, will match:
/admin/page/login/
/admin/page/login
admin/page/login
http://www.dot.com/admin/page/login
/admin/page/login?id=10
/admin/page/login/id/10
/admin/page/login/?id=10
/admin/page/login/user?id=10
/admin/page/login/user/?id=10
page/login
page/login/
page/login/id/10
/page/login/id/10
And these not:
/admin/firstpage/login
admin/page/loginOk
/admin/page/loginOk/id/10
mypage/login/id/10
/mypage/login/id/10
mypage/login
I tried: page\/login[\/\s\?], \/?page\/login[\/\s\?] without any result
You can use a word boundary so partial matches aren't matched.
\bpage\/login[\/\s?]
Demo: https://regex101.com/r/yhNsdw/1/
Also if you change your delimiter none of the forward slashes will need to be escaped.

Regular expression to replace all url from string but skip one

I have regular expression that's is removing all url from a string but I want to change this and add exception for my site link.
$url = 'This is url for example to remove www.somewbsite.com but i want to skip removing this url www.mywebsite.com';
$no_url = preg_replace("/(https|http|ftp)\:\/\/|([a-z0-9A-Z]+\.[a-z0-9A-Z]+\.[a-zA-Z]{2,4})|([a-z0-9A-Z]+\.[a-zA-Z]{2,4})|\?([a-zA-Z0-9]+[\&\=\#a-z]+)/i", "★", $url);
First of all, since you are replacing with a hard-coded symbol, and you are using a case-insensitive modifier, your regex can be reduced to
'~(?:https?|ftp)://|(?:[a-z0-9]+\.)?[a-z0-9]+\.[a-z]{2,4}|\?[a-z0-9]+[&=#a-z]+~i'
whatever it means to match. Note that 2 alternatives here were too similar ([a-z0-9A-Z]+\.[a-z0-9A-Z]+\.[a-zA-Z]{2,4})|([a-z0-9A-Z]+\.[a-zA-Z]{2,4}), they are merged into 1 with the help of an optional non-capturing group ((?:[a-z0-9]+\.)?).
Now, if you want to avoid matching a specific pattern, you may use a SKIP-FAIL technique: match what you want to preserve and skip it.
'~www\.mywebsite\.com(*SKIP)(*FAIL)|(?:https?|ftp)://|(?:[a-z0-9]+\.)?[a-z0-9]+\.[a-z]{2,4}|\?[a-z0-9]+[&=#a-z]+~i'
See this regex demo.

How to write such url pattern?

I need URL pattern for my router which would match with:
/page_name.html
/page_name.html/1
/page_name.html/2
....
/page_name.html/999
And preg_match() must put page_name into matches[1] and digit after slash into matches[2] (or empty string, index [2] must always be present!).
I need this to not match my patern:
/page_name.html/
/page_name.html131
I wrote this:
^\/([\w\-]+)\.html[\/]?([\d]{1,3})?$/
But it mathces URLs like /page_name.html123 and doesn't put anything into matches[2] if there is no digit.
You can use this regex:
preg_match('~^/([\w-]+)\.html(?|/(\d{1,3})|())$~', $matches, $input);
RegEx Demo
(?|...) - Subpatterns declared within each alternative of this construct will start over from the same index. This is to make sure to always populate $matches[2] with something, even an empty string.

Regex to match a section between two static url components

I have a url like so: http://example.com/c/TEXTTOMATCH/. The problem is that the url isn't always like that; sometimes it's http://example.com/c/TEXTTOMATCH/#/?test. I'm trying to use a regex to grab everything between /c/ and /. I've tried
$catpreg = preg_match('/c(.*)/', $reffer, $matches);
but it fails.
How about this:
<?php
$url='http://example.com/wreqwreqrq/rfqewrqwe/c/TEXTTOMATCH/';
$split_url=parse_url($url, PHP_URL_PATH);
//print_r($split_url);
$e=explode('/',$split_url);
//find "c" key and add one
$find=array_search('c',$e);
echo $e[$find+1];
Try this:
preg_match('#/c/(.*?)/#', $reffer, $matches);
You were just everything after c, not matching the slashes. The slashes in your call were being used as the delimiters around the regexp, I used # as the delimiters so I could use / inside the regexp without having to escape them.
The non-greedy quantifier .*? ensures that it only matches TEXTTOMATCH in the second example, not TEXTTOMATCH/#.

PHP Remove all after particular pattern

I want to remove all characters after a particular pattern from a string (url). Following are some example urls.
http://www.example.com/profile/aaa-bbb/Group
http://www.example.com/profile/ccc-ddd/Group?tab=23
http://www.example.com/profile/Group-sss-t/Group
http://www.example.com/profile/ppp-qqq/
I need the output as,
http://www.example.com/profile/aaa-bbb/
http://www.example.com/profile/ccc-ddd/
http://www.example.com/profile/Group-sss-t/
http://www.example.com/profile/ppp-qqq/
Here actually i need to remove all characters after Group, but in the third utl there Group is present twice. Dont know how to handle this. Help please, thanks in advance
Something like this should do the trick ( removes everything after the last / )
$newUrl = preg_replace('/(.*)\/.*$/', '$1/', $url);
See: http://phpfiddle.org/main/code/j7c-8gx and hit F9 to see the result of url: 'http://www.example.com/profile/ccc-ddd/Group?tab=23'
I would use strrpos witch finds the position of a substring but starts from the end:
if(strpos($url,"Group")!==false){
$url = substr($url,0,strrpos($url,"Group"));
}
%(http://www.example.com/profile/[^/]+/)%
Matches http://www.example.com/profile/ followed by one of these groups.
So preg_match_all('%(http://www.example.com/profile/[^/]+/)%', $urls, $matches) saves the matched parts in $matches.

Categories