Jump to content

Want to extract page title from url


astarmathsandphysics

Recommended Posts

I have several websites using the same basic script and I have the same problem in each - all the page titles are the same.

see

http://courseworkbank.info/?dir=GCSE

the page title here is COURSEWORKBANK.INFO

I would like it to be GCSE

For http://courseworkbank.info/?dir=GCSE/Biology

I would like it to be GCSE - Biology

How do I do this?

Am using a simple index.php file to load everything.

Link to comment
https://forums.phpfreaks.com/topic/294548-want-to-extract-page-title-from-url/
Share on other sites

Your structure seems to follow a pattern of '?dir=DIR1/DIR2/DIR3 etc, and you want to display a title of DIR1 - DIR2 - DIR3, the following code should suffice.

if ( isset( $_GET['dir'] ) )
{
  $explode = explode( '/', $_GET['dir'] );
  $dir_count = count( $explode );
  $title = '';
  $i = 1;

  foreach ( $explode as $k )
  {
    $suffix = ( $i == $dir_count ) ? '' : ' - '; // Seperate the directory names with -
    $title .= $k . $suffix;
    $i++;
  }
}

If you place this code before your title code (e.g. <title>COUR...</title>)

 

Then change your title code to <title><?= $title; ?></title>, it should work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.