Jump to content

using part page url as reference


robhargreaves

Recommended Posts

Hi

 

Please can someone tell me if possible and how to reference part of a page url in

 

<code>

<?php include('includes/content.php'); ?>

</code>

 

so for example if my page is called news.php is it possible to prefix content.php and make it automatically news_content.php in the code.

 

I hope this makes sense!

 

Thanks Rob

Link to comment
https://forums.phpfreaks.com/topic/280364-using-part-page-url-as-reference/
Share on other sites

Hi I have found using this code works well

 

<code>

<?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);
}

?>

</code>

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.