PHP - preg_match regex - php

I am trying to use preg_match to figure out if a url has certain pattern and right now its not working as I expected. Heres what I have so far:
if(! preg_match('^lease/([0-9]+)/?', $url)) {
wp_redirect( home_url(), 301 );
}
Basically I want to see if the url pattern is as below(lease keyword followed by a number) and if not the page should be redirected to homepage. Im not good with regex so I need some help with this one. TIA.
www.example.com/lease/324

You need to allow any chars before the /lease with .*?, an end of string anchor $ and regex delimiters (I prefer ~ so as not to escape forward slashes):
if(! preg_match('~^.*/lease/([0-9]+)/?$~', $url)
Or you may omit ^.*? part since preg_match allows partial matches
if(! preg_match('~/lease/([0-9]+)/?$~', $url)

You don't need the capture group (parenthesis) unless you want to know what the trailing numbers are. But looks like you just want to check if it contains lease/{number}
You can try this:
if (! preg_match("/lease\/[0-9]+/", $url)) {
wp_redirect( home_url(), 301);
}
contains lease, then a slash, then 1+ number

Related

Regex to match OPO Invite link

if (preg_match('#^https?://account.oneplus.net/invite/claim/....-....-....-....#', $url) === 0) {
return "Invalid link";
}
I currently use this code (in PHP) to verify the url. However, it also passes as true when you try with other stuff trailing behind the link. How do I fix this so that only links ending with or without / work?
This was the regex I was looking for:
preg_match('#^https?://account.oneplus.net/invite/claim/\S{4}-\S{4}-\S{4}-\S{4}/?$#', $url) === 0
I suggest you to replace all the dots with \S (which matches any non-space character), so .... would be written as \S{4} because . would match also a horizontal space. And also add the pattern (/?) to match an optional / at the last.

php regex match word and stop

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'])

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.

PHP Regex - Issue with forward slashes and alternation

I have a series of URLs like so:
http://www.somesite.com/de/page
http://www.somesite.com/de/another
http://www.somesite.com/de/page/something
http://www.somesite.com/de/page/bar
I need to search the block of text and pull the language and am using a regex like so:
/(de|en|jp)/
I'm trying to find and replace, via preg_replace and including the forward slashes:
/de/
/en/
/jp/
However, this doesn't work and does not include the slashes. I've tried escaping the slashes with \, \\. I've tried placing the needle in preg_quote but this breaks the alternation.
I feel like I am missing something very simple here!
edit:
Full function call:
preg_replace("/(de|en|jp)/", "/".$newLang."/", $url);
--
(tagged magento and wordpress as I am trying to solve an issue with unifying the navigation menu when both CMSes are multilingual)
You don't have to use slashes as delimiters, but you have to have some delimiter. Try this:
if( preg_match("(/(de|en|jp)/)",$url,$m)) {
$lanuage = $m[1];
}
You can use a different delimiter, such as %.
if (preg_match('%/(de|en|jp)/%', $url, $match)) {
$lang = $match[1];
}
That should help you, just modify what you have :).

Regex - find, rewrite and replace tag in text

I need to catch custom "tag" in text string, rewrite and replace it.
It looks like:
<mytag=http://url.com/file.php?some=variable&another=variable>
First I need to catch url from tag, then I rewrite it with my function and then replace whole tag with new url. Can anybody help me how to catch and replace it? In text can be more tags with various url's.
That's trivial. You have enough context and anchors for that. And basically using preg_replace or preg_repace_callback (for more complex replacement schemes) works as follows:
$src = preg_replace('~ <mytag= (http://[^>]+) > ~smix', '<a href=$1>$1</a>', $src);
The whitespace here are decorative. Crucial is the [^>] for not matching to much, and ( and ) for capturing the URL as $1.
See also Open source RegexBuddy alternatives and Online regex testing for some helpful tools, or RegExp.info for a nicer tutorial.
You can try this:
$my_tag = "<mytag=http://url.com/file.php?some=variable&another=variable>";
preg_match( "/<mytag=(.*)>/", $my_tag, $matches );
$new_tag = str_replace( $matches[1], "http://newurl.com", $my_tag);
The $new_tag variable will end up with the following:
<mytag=http://newurl.com>

Categories