Reputation: 413
I would like to match a portion of a URL in this order.
First the domain name will remain static. So, nothing check with regex.
$domain_name = "http://foo.com/";
What I would like to validate is what comes after the last /
.
So, my AIM is to create something like.
$stings_only = "[\w+]";
$number_only = "[\d+]";
$numbers_and_strings = "[0-9][a-z][A-Z]";
Now, I would like to just use the above variables to check if a URL confirms to the patterns mentioned.
$example_url = "http://foo.com/some-title-with-id-1";
var_dump(preg_match({$domain_name}{$strings_only}, $example_url));
The above should return false, because title is NOT $string_only.
$example_url = "http://foo.com/foobartar";
var_dump(preg_match({$domain_name}{$strings_only}, $example_url));
The above should return true, because title is $string_only.
Upvotes: 0
Views: 154
Reputation: 20486
Update:
~^http://foo\.com/[a-z]+/?$~i
~^http://foo\.com/[0-9]+/?$~
~^http://foo\.com/[a-z0-9]+/?$~i
These would be your three expressions to match alphabetical URLs, numeric URLS, and alphanumeric. A couple notes, \w
matches [a-zA-Z0-9_]
so I don't think it is what you expected. The +
inside of your character class ([]
) does not have any special meaning, like you may expect. \w
and \d
are "shorthand character classes" and do not need to be within the []
syntax (however they can be, e.g. [\w.,]
). Notice the i
modifier, this makes the expressions case-insensitive so we do not need to use [a-zA-Z]
.
$strings_only = '~^http://foo\.com/[a-z]+/?$~i';
$url = 'http://foo.com/some-title-with-id-1';
var_dump(preg_match($strings_only, $url)); // int(0)
$url = 'http://foo.com/foobartar';
var_dump(preg_match($strings_only, $url)); // int(1)
Test/tweak all of my above expressions with Regex101.
.
matches any character, but only once. Use .*
for 0+ or .+
for 1+. However, these will be greedy and match your whole string and can potentially cause problems. You can make it lazy by adding ?
to the end of them (meaning it will stop as soon as it sees the next character /
). Or, you can specify anything but a /
using a negative character class [^/]
.
My final regex of choice would be:
~^https://stolak\.ru/([^/]+)/?$~
Notice the ~
delimiters, so that you don't need to escape every /
. Also, you need to escape the .
with \
since it has a special meaning. I threw the [^/]+
URI parameter into a capture group and made the trailing slash optional by using /?
. Finally, I anchored this to the beginning and the end of the strings (^
and $
, respectively).
Your question was somewhat vague, so I tried to interpret what you wanted to match. If I was wrong, let me know and I can update it. However, I tried to explain it all so that you could learn and tweak it to your needs. Also, play with my Regex101 link -- it will make testing easier.
Implementation:
$pattern = '~^https://stolak\.ru/([^/]+)/?$~';
$url = 'https://stolak.ru/car-type-b1';
preg_match($pattern, $url, $matches);
var_dump($matches);
// array(2) {
// [0]=>
// string(29) "https://stolak.ru/car-type-b1"
// [1]=>
// string(11) "car-type-b1"
// }
Upvotes: 2