B
B
billgeits9764552015-08-31 11:03:27
PHP
billgeits976455, 2015-08-31 11:03:27

How to write a regular expression in PHP to validate a URL?

Help how to write a regular expression in php. To check the URL of a page. For example, the user entered a link, it must be checked. But it is assumed that the link is always of this type mysite.com/# and then 5 characters of letters or numbers of the English alphabet. No matter how I tried to implement it, it doesn't work.
Thank you in advance for your help!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaliy Orlov, 2015-08-31
@billgeits976455

1) You can for example like this

$url = 'http://mysite.com/#something';
$base  ='http://mysite.com/#';

if (preg_match('~^'.preg_quote($base).'[a-z0-9]{5}$~i', $url)) {
  echo 'Valid';
} else {
echo 'Invalid';
}

2) Or you can use this function php.net/manual/en/function.parse-url.php and check it in parts.

C
ChernovGV, 2015-08-31
@ChernovGV

I propose to read habrahabr.ru/post/115825

K
krypt3r, 2015-08-31
@krypt3r

filter_var() with FILTER_VALIDATE_URL option

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question