1

I have hundreds of pages, each with a unique url. All the pages are the same with the exception of the url which is also the name of the page. How can i get the page to read the url and place the name (parsing the http://www. and the .tld? It needs to be both the Title of the page as well as be shown else where on the page. Can this be done in PHP?

clamchoda
  • 4,411
  • 2
  • 36
  • 74
wordcutter
  • 21
  • 1
  • 2
    Can you show which parts of the URL will form the name? Can you show an example? – Pekka Apr 20 '11 at 21:53
  • So, If your URL was: http://www.http://www.wordcutter.tld, you want to parse and set the attribute of your page as "wordcutter"? – clamchoda Apr 20 '11 at 21:57

2 Answers2

2

If you use this code it will only grab the domain name without the http://www. You can then use a substring query to remove the .tld.

<?php
# Using HTTP_HOST

$domain = $_SERVER['HTTP_HOST'];
$domain = substr($domain, 0, -4);

echo "<title>" . $domain . "</title>";
?>

If you place this code in between the tags , then you will get a dynamic title based on the URL.

Tyler Ferraro
  • 3,753
  • 1
  • 21
  • 28
  • here is just a quick html mark up of what a page blank looks like parsed url here

    Welcome to " url name inserted here "

    – wordcutter Apr 20 '11 at 22:15
  • here is a sample link to one of the pages... http://858.414.6744.us/demo2.php you can see it is cutting off the last number in the sequence – wordcutter Apr 20 '11 at 22:16
  • Thank you for all the help.. the title is working perfect. now if i can get the domain name inserted into the page all would be right on track. can i use the same call used to get the title or are the tags different when they are in the body of the page? – wordcutter Apr 20 '11 at 22:29
  • The php code will remain the same whether it's in the head or the body. However you only use html tags in the section. – Tyler Ferraro Apr 20 '11 at 22:31
1

Add the following code to a page:

<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
?>

You can now get the current page URL using the line:

<?php
  echo curPageURL();
?>

check PHP: How to Get the Current Page URL

I didn't understand the rest of your question.

Bastardo
  • 4,144
  • 9
  • 41
  • 60
  • Now that we have the url as the title, can that same info be passed to other locations on the page ie, " welcome to xxxxxxxxx " xxxxxx being the parsed section of the url being used as the title? – wordcutter Apr 20 '11 at 22:12
  • I think you can do that pal. you can even turn $pageURL to a tring and use it too, check this : http://stackoverflow.com/questions/28098/php-tostring-equivalent – Bastardo Apr 20 '11 at 22:28