Avatar of cemlouis
cemlouisFlag for Türkiye

asked on 

How to pick domain name from an url string with php regex?

Hello,

I have a problem, I have several url strings like below:


http://www.mysite.com/?action=1234
http://www.mysite.com/doit.php?=abc
http://www.mysite.com/category/myarticle/
http://mysite.com/myresearch/15.html
http://subdomain.mysite.com/thelist/abcd.php
...
..
.


The list goes on and on, is there any regex func that fits all the endless posibilities to pick up the domain name "mysite.com" from the url strings?

Any help greatly appreciated...
Thx
PHP

Avatar of undefined
Last Comment
cemlouis
Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

regex is unnecessarily complicated for this.  A simplier solution is

strpos( )
http://us2.php.net/manual/en/function.strpos.php

$position = strpos($url, 'mysite.com');

will give you the starting position of your domain within the url string.
Avatar of cemlouis
cemlouis
Flag of Türkiye image

ASKER

The thing is not only mysite.com it can be yoursite.com or yoursite.co.uk etc...

Below you can find the edited question:

Hello,

I have a problem, I have several url strings like below:


http://www.mysite.com/?action=1234
http://www.mysite.co.uk/doit.php?=abc
http://www.mysite.com/category/myarticle/
http://mysite.com/myresearch/15.html
http://subdomain.mysite.com/thelist/abcd.php
http://mysite.co.uk/book/booking123.php
...
..
.


The list goes on and on, is there any regex func that fits all the endless posibilities to pick up the domain name "mysite.com" or "mysite.co.uk" or "yoursite.co.uk" or any other domain names from the url strings?

Any help greatly appreciated...
Thx
Avatar of JamesCssl
JamesCssl
Flag of United States of America image

$string = 'http://subdomain.mysite.com/thelist/abcd.php';
preg_match('/[a-zA-Z0-9\-]+\.[a-zA-Z]{2,6}(?=\/)/',$string,$matches);
echo $matches[0];
Avatar of cemlouis
cemlouis
Flag of Türkiye image

ASKER

Thank you James but,

The below is giving "co.uk" not "mysite.co.uk"

$string = 'http://mysite.co.uk/thelist/abcd.php';
preg_match('/[a-zA-Z0-9\-]+\.[a-zA-Z]{2,6}(?=\/)/',$string,$matches);
echo $matches[0];

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of JamesCssl
JamesCssl
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of cemlouis
cemlouis
Flag of Türkiye image

ASKER

James,

You are really fast,

The below one gives "mysite.co.uk" which is very good

$string = 'http://deepersubdomain.subdomain.my.co.uk/book/booking123.php';
preg_match('/[a-zA-Z0-9\-]+\.[a-zA-Z\.]{2,6}(?=\/)/',$string,$matches);
echo $matches[0];

but the below one gives "game.tw"

$string = 'http://deepersubdomain.subdomain1.my.game.tw/book/booking123.php';
preg_match('/[a-zA-Z0-9\-]+\.[a-zA-Z\.]{2,6}(?=\/)/',$string,$matches);
echo $matches[0];

Any ideas? Thank you by the way...
Avatar of JamesCssl
JamesCssl
Flag of United States of America image

do you want it to include the subdomains?
Avatar of cemlouis
cemlouis
Flag of Türkiye image

ASKER

The below one gives "mysite.co.uk" which is very good

will be

The below one gives "my.co.uk" which is very good

for the previous post...
Avatar of cemlouis
cemlouis
Flag of Türkiye image

ASKER

No I want to display "my.game.tw" (domain name) for that...
Avatar of JamesCssl
JamesCssl
Flag of United States of America image

$string = 'http://deepersubdomain.subdomain1.my.game.tw/book/booking123.php';
preg_match('/[a-zA-Z0-9\-]+\.[a-zA-Z\.]{2,7}(?=\/)/',$string,$matches);
echo $matches[0];
Avatar of cemlouis
cemlouis
Flag of Türkiye image

ASKER

ok give me a few mins things are getting more complicated... I will write clearly what I want...
Avatar of cemlouis
cemlouis
Flag of Türkiye image

ASKER

Thank you for your efforts...
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo