Harea Costicla Harea Costicla - 1 year ago 100
PHP Question

Get only a specified url

I have a question. I want to check only sites finished in :


I tried :

if((preg_match('.\w+.test.com', $_SERVER['HTTP_ORIGIN'])) or (preg_match('.\w+.test1.com', $_SERVER['HTTP_ORIGIN']))){

Answer Source

For starters, you want the most reliable way to break down any given url. For this, you can use PHP's built-in parse_url function:

$host = parse_url($url, PHP_URL_HOST);

Then, you want to check whether or not it ends in test.com or test1.com, which you can do using a regex:

if (preg_match('/test1?\.com$/', $host)) {

The regex works like this:

  • test: matches a string literal "test"
  • 1?: Matches a literal 1, but it's optional, so both test and test1 will match
  • \.: a literal dot match
  • com: literal match for com
  • $: end of string. The expression will only match if the string ends in test.com or test1.com.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download