2

Currently I'm removing https:// and http:// with following code:

$r=\Input::get('searchQuery');
        
        $Query = preg_replace( "#^[^:/.]*[:/]+#i", "", $r);
        $Query=rtrim($Query, "/");

I need to remove www. as well. What should I add to the code inorder to remove www.
Eg: When I search for https://www.example.com/ It should only search for example.com

John Lobo
  • 14,355
  • 2
  • 10
  • 20
  • 2
    check this https://stackoverflow.com/questions/16027102/get-domain-name-from-full-url @stanev01 answer – John Lobo Jul 18 '21 at 13:11
  • you can simply do separate if the string is either `https://www.example.com/` or `http://www.example.com/`, use `Str::of($str)->after('https://www.');}` see here https://laravel.com/docs/8.x/helpers#method-fluent-str-after – bhucho Jul 18 '21 at 13:18
  • 2
    Does this answer your question? [Get domain name from full URL](https://stackoverflow.com/questions/16027102/get-domain-name-from-full-url) – nice_dev Jul 18 '21 at 13:27
  • Are you just wanting the www. removing or does it also need to remove other subdomains? i.e. `www.example.com` becomes `example.com`, should `sub1.example.com` remain the same or become `example.com` – mic Jul 18 '21 at 13:49
  • Did none of all the answers help you ? – matiaslauriti Jul 24 '21 at 19:34
  • Could you please check it - https://stackoverflow.com/questions/6336281/php-remove-www-from-url-inside-a-string – Dmitry Leiko Jul 26 '21 at 08:08
  • @nortonvuv Are you alive ? Did any of our answers help ? – matiaslauriti Jul 29 '21 at 23:04

5 Answers5

3

It is really easy, you can use preg_replace with a simple regex:

$query = preg_replace('/^((http:\/\/|https:\/\/)?(www\.)?)/i', '', $text);

See this example, so you can understand my regex.

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
0

The following regex works on any string with an optional "http(s)", "www." and trailing slash:

$r=\Input::get('searchQuery');
$Query = preg_replace('/^(https?\:\/\/)?(www.)?|\/$/', "", $r);

How it works:

^ start of the string.

(https?\:\/\/)? optionally match "http(s)://" (the "s" is also optional).

(www.)? optionally match: "www.".

|\/$ match "/" if it is the last character of the string.

Dirk J. Faber
  • 4,360
  • 5
  • 20
  • 58
0

You can use Laravel Helpers Method preg_replace_array. preg_replace_array function replaces a given pattern in the string sequentially useing an array:

$string = 'https://www.example.com/'
$query = preg_replace_array('#^[^:/.]*[:/www.?]+#i', [''], $string); // example.com/
$query = rtrim($query, '/'); // example.com

Input: https://www.example.com/
Output: example.com

Sarwar Ahmed
  • 588
  • 2
  • 6
  • 14
0
$r=\Input::get('searchQuery');
$query = preg_replace_array('#^[^:/.]*[:/www.?]+#i', [''], $r);  
$query = rtrim($query, '/'); // google.com

Your OUT PUT will be google.com

I hope You Got Your Solution .

Rakesh kumar Oad
  • 1,332
  • 1
  • 15
  • 24
0

code might get bit long but this works everytime, you can add more fuctionality by getting position of / inside of string , working code as below

 <?php
       // $r=\Input::get('searchQuery');
       // $searchQuery = $r; 

       $searchQuery = "www.google.com";

        $searchQuery = str_replace("https://","",$searchQuery);
        $searchQuery = str_replace("http://","",$searchQuery);
      $searchQuery = str_replace("www.","",$searchQuery);
            ?>
<?php #to remove only last / from string
         $lastchar = substr($searchQuery , -1);
    $length = strlen($searchQuery);

    if ($lastchar == "/")
    {
    $searchQuery  = substr_replace($searchQuery, "" , -1);
    }
    else {
    $searchQuery  = $searchQuery ;
    }
    echo $searchQuery; ?>
Shubham Dange
  • 173
  • 13