Carlo
Carlo

Reputation: 1

Create Meta tag from reading data in URL using PHP

I need help with the following please:

I have one PHP page that reads from a database and creates 1000's of pages - too many to create meta data for each page. I need to create meta data from the info in the url:

Instance 1:
http://dom.com/page.php/area1/type1
http://dom.com/page.php/area1/type2

Instance 2:
http://dom.com/page.php/area1/type1/name
http://dom.com/page.php/area1/type2/proper%20name

Area = 1-30 (lookup from list provided)
Type = 1-2 (lookup from list provided)
Name = strip "%20" for spaces in names

Populate: Instance 1:

<title>Type, Area</title>
<meta content="Type, Area" name="description" 

Instance 2:

<title>Proper Name, Type, Area</title>
<meta content="Proper Name, Type, Area" name="description" />

Any help with be highly appreciated!

Upvotes: 0

Views: 768

Answers (1)

Christof
Christof

Reputation: 3927

Carlo, This should work (paste it where your meta tag should be):

<?php
$url = parse_url($_SERVER['REQUEST_URI']);
// if url has a path (/whatever/somethingelse)
if (isset($url['path'])
  {
  // split the path on every slash
  $a_path = explode('/', $url['path']);
  // reverse the order of the parts, join them with commas and replace %20
  $info = str_replace('%20', ' ', join(', ', array_reverse($a_path)));
  // print info to page
  print '<title>' . $info . '</title>';
  print '<meta content="' . $info . '" name="description" />';
  }
?>

Although I agree that pasting the title in the meta description probably isn't the best idea.

Edit: follow-up on your comment:

// pop off the last value of the path parts
$info = str_replace('%20', ' ', array_pop($a_path));

Upvotes: 1

Related Questions