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
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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.