Viewed   182 times

What I want

I want to get from a URL the domain part so from ->


| input                                        | output                |
|   | |
|                |      |
|               |        |
|                    |             |

I found some related questions in but none of them was exactly what I was looking for.

Thanks for any help!



There's no need to use a regex for this. PHP has an inbuilt function to do just this. Use parse_url():

$domain = parse_url($url, PHP_URL_HOST);
Tuesday, December 27, 2022

John Gruber, of Daring Fireball fame, had a post recently that detailed his quest for a good URL-recognizing regex string. What he came up with was this:


Which apparently does OK with Unicode-containing URLs, as well. You'd need to do the slight modification to it to get the rest of what you're looking for -- the scheme, username, password, etc. Alan Storm wrote a piece explaining Gruber's regex pattern, which I definitely needed (regex is so write-once-have-no-clue-how-to-read-ever-again!).

Saturday, December 3, 2022

According to the documentation:

preg_match_all — Perform a global regular expression match

Since you are after just one, you should be using preg_match:

Perform a regular expression match

$regex = '/https?://[^" ]+/i';
$string = "lorem ipsum lorem ipusm dolor";
preg_match($regex, $string, $matches);
echo $matches[0];

Sunday, November 20, 2022

Have your complete .htaccess like this:

  Options +FollowSymLinks -MultiViews
  # Turn mod_rewrite on
  RewriteEngine On
  RewriteBase /

  ## don't touch /forum URIs
  RewriteRule ^forums/ - [L,NC]

  RewriteCond %{THE_REQUEST} s/+products(?:.php)??id=([0-9]+) [NC]
  RewriteRule ^ products/%1? [R,L]

  RewriteRule ^products/([0-9]+)/?$ products.php?id=$1 [L,QSA]

  ## hide .php extension snippet
  # To externally redirect /dir/foo.php to /dir/foo
  RewriteCond %{THE_REQUEST} s([^.]+).php [NC]
  RewriteRule ^ %1 [R,L]

  # To internally forward /dir/foo to /dir/foo.php
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}.php -f
  RewriteRule ^(.+?)/?$ $1.php [L]
Saturday, November 12, 2022

You can use this regex:



$arr = array(
'This is really cool site: isn't it?',
'Hello! You can visit for some awesome pictures, or just go to by itself',
'Hello! Check out my site at!',
'Hello.Come visit!',

foreach($arr as $url) {   
   $link = preg_replace_callback('#(s|^)((?:https?://)?w+(?:.w+)+(?<=.(net|org|edu|com))(?:/[^s]*|))(?=s|b)#is',
           create_function('$m', 'if (!preg_match("#^(https?://)#", $m[2]))
               return $m[1]."<a href="http://".$m[2]."">".$m[2]."</a>"; else return $m[1]."<a href="".$m[2]."">".$m[2]."</a>";'),
   echo $link . "n";


<a href=""></a>
This is really cool site: <a href=""></a>/ isn't it?
<a href=""></a>
<a href=""></a>
Hello! You can visit <a href=""></a> for some awesome pictures, or just go to <a href=""></a> by itself
<a href=""></a>
<a href=""></a>
Hello! Check out my site at <a href=""></a>!
Hello.Come visit <a href=""></a>!
<a href=""></a>

PS: This regex only supports http and https scheme in URL. So eg: if you want to support ftp also then you need to modify the regex a little.

Sunday, August 7, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :