robhargreaves Posted July 21, 2013 Share Posted July 21, 2013 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted July 21, 2013 Share Posted July 21, 2013 Is news.php a real file? How about news_content.php? Try this: What do you want the URL to look like and what kind of code does it have to "execute"? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 21, 2013 Share Posted July 21, 2013 Not sure what this has to do with a URL. $base = basename(__FILE__, ".php"); include("includes/{$base}_content.php"); To use the filename in the actual URL you could use something from the $_SERVER array. Quote Link to comment Share on other sites More sharing options...
robhargreaves Posted July 21, 2013 Author Share Posted July 21, 2013 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 existsif(file_exists($include_file)) { include($include_file);}?> </code> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.