1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Passing Variables in PHP - Noob Question

Discussion in 'PHP & Perl' started by Reginald, Feb 23, 2010.

  1. Reginald

    Reginald Newbie

    Joined:
    Jan 6, 2010
    Messages:
    3
    Likes Received:
    0
    Hi all,

    I have a quick question on passing information from query strings. I have a webpage that has URL information passed to it i.e.

    www. domain.com/?URL=www.test.com/page1/page2/page3.html

    What I would like to do is grab the information displayed in the URL string and pass it into the webpage, either in a link or text. Here's the kicker though. I don't want the entire URL to be displayed in the webpage, I only want the root domain i.e. test.com, without http or www. or any subdomains, subpages or ugly query strings.

    I've searched high and low for the answer and can't find it anywhere. If someone could spell out the exact code required to do all this i.e. firstly grabbing the URL and inserting it into the webpage and secondly 'cleaning' it up, I would be most grateful. Apologies if this is a noob question but my php knowledge is very limited.

    Thanks in advance for your help,
    Reginald.
     
  2. showboytridin

    showboytridin Regular Member

    Joined:
    Sep 5, 2009
    Messages:
    348
    Likes Received:
    714
    Location:
    127.0.0.1
    You have to use regular expressions for that. You can try:

    Code:
    <?php
    
    $url_info = "www.test.com/page1/page2/page3.html"; //or $url_info = $_GET['URL']; 
    
    preg_match('@^(?:http://)?([^/]+)@i',
        $url_info, $matches);
    $host = $matches[1];
    
    preg_match('/[^.]+\.[^.]+$/', $host, $matches);
    echo "domain name is: ".$matches[0];
    ?>
    
     
  3. Reginald

    Reginald Newbie

    Joined:
    Jan 6, 2010
    Messages:
    3
    Likes Received:
    0
    Hi showboytridin,

    Thanks for your reply. Your suggestions works fine and cleans up whatever URL is inserted into the $url_info = part. However since the URL variable will vary all the time it can't be static as in your example. So what I was thinking was attaching a simple GET function that retrieves whatever URL has been passed and echoes it into the $url_info = part to be cleaned. Below is what I thought might work. The only problem is it doesn't. Can you see where this breaks down?

    Code:
    <?php
      if ($_GET['keyword'] != "") {
        $keyword=ucwords(str_replace('-',' ',$_GET['keyword']));
      } else {
      $keyword="";
    }
    ?>
    
    <?php
    
    $url_info = "<?php echo $keyword; ?>"; //or $url_info = $_GET['URL']; 
    
    preg_match('@^(?:http://)?([^/]+)@i',
        $url_info, $matches);
    $host = $matches[1];
    
    preg_match('/[^.]+\.[^.]+$/', $host, $matches);
    echo "domain name is: ".$matches[0];
    ?>
     
  4. aмillionaírе

    aмillionaírе Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 20, 2008
    Messages:
    532
    Likes Received:
    358
    PHP:
    <?php
    $i 
    preg_replace('/http\:\/\/www\./i',"",$_GET['url']);
    $domain preg_replace('/\/\S+/i',"",$i);
    echo 
    'domain name is: ' .$domain
    ?>
     
  5. showboytridin

    showboytridin Regular Member

    Joined:
    Sep 5, 2009
    Messages:
    348
    Likes Received:
    714
    Location:
    127.0.0.1
    Your url must be something like:

    www. domain.com/?keyword=www.test.com/page1/page2/page3.html

    Code:
    <?php
      if ( isset($_GET['keyword']) ) {
        $keyword = $_GET['keyword'];
      } else {
      $keyword="";
    }
    ?>
    
    <?php
    
    $url_info = "<?php echo $keyword; ?>"; //or $url_info = $_GET['URL']; 
    
    preg_match('@^(?:http://)?([^/]+)@i',
        $url_info, $matches);
    $host = $matches[1];
    
    preg_match('/[^.]+\.[^.]+$/', $host, $matches);
    echo "domain name is: ".$matches[0];
    ?>
    
     
  6. Reginald

    Reginald Newbie

    Joined:
    Jan 6, 2010
    Messages:
    3
    Likes Received:
    0
    Thanks a&#1084;illionaír&#1077;. That worked perfectly!