Passing Variables in PHP - Noob Question

Reginald

Newbie
Joined
Jan 6, 2010
Messages
3
Reaction score
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.
 
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];
?>
 
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];
?>
 
PHP:
<?php
$i = preg_replace('/http\:\/\/www\./i',"",$_GET['url']);
$domain = preg_replace('/\/\S+/i',"",$i);
echo 'domain name is: ' .$domain; 
?>
 
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];
?>
 
Back
Top