using part page url as reference

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
MarioParty
New php-forum User
New php-forum User
Posts: 42
Joined: Tue Aug 02, 2011 2:52 am

Sun Jul 21, 2013 2:56 am

Code: Select all

<?php
// Get the current page filename
$include_file = __FILE__;

// Remove the file extension .php (or any other file extension)
function remove_extension($filename) {
  $ext = pathinfo($filename, PATHINFO_EXTENSION);
  return preg_replace('/\.' . preg_quote($ext, '/') . '$/', '', $filename);
}
$include_file = remove_extension($include_file);

// Add "_content.php"
$include_file .= '_content.php';

// Include the file if it exists
if(file_exists($include_file)) {
  include($include_file);
}
?>
I haven't tested this, but it should work.

There are many other ways of doing this, but this is a solution that matches the requirements.

MarioParty
New php-forum User
New php-forum User
Posts: 42
Joined: Tue Aug 02, 2011 2:52 am

Sun Jul 21, 2013 3:00 pm

I don't really understand what you are asking about $aboutmesubtitle1 and $filenamesubtitle1.

Here is some altered code to help you do what you need to

news.php:

Code: Select all

<?php
// Get the current page filename
$include_file = __FILE__;
// $include_file = news.php

// Include news_content.php
include('content.php');
?>
content.php:

Code: Select all

<?php
// Remove the file extension .php (or any other file extension)
function remove_extension($filename) {
  $ext = pathinfo($filename, PATHINFO_EXTENSION);
  return preg_replace('/\.' . preg_quote($ext, '/') . '$/', '', $filename);
}
$include_file = remove_extension($include_file);
// $include_file = news

// Add "_content.php"
$include_file .= '_content.php';
// $include_file = news_content.php

// Include the file if it exists
if(file_exists($include_file)) {
  include($include_file);
}
// include('news_content.php');
?>

MarioParty
New php-forum User
New php-forum User
Posts: 42
Joined: Tue Aug 02, 2011 2:52 am

Sat Aug 03, 2013 3:51 am

What you need to do is something like this:

functions.php

Code: Select all

// Function to get the page name without the extension
function get_page_file_name_without_extension($file_name = __FILE__) {
  // Get the current page filename
  $include_file = $file_name;

  // Remove the file extension .php (or any other file extension)
  return $include_file = remove_extension($include_file);
}

// Function to remove the file extension .php (or any other file extension)
function remove_extension($filename) {
  $ext = pathinfo($filename, PATHINFO_EXTENSION);
  return preg_replace('/\.' . preg_quote($ext, '/') . '$/', '', $filename);
}
 
example.php

Code: Select all

<?php
include('functions.php');

$page_name = get_page_file_name_without_extension(__FILE__);
?>
<meta name="description" content="<?php echo ${$page_name . 'metadescription'}; ?>" />

Post Reply