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 Link to comment https://forums.phpfreaks.com/topic/280364-using-part-page-url-as-reference/ 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"? Link to comment https://forums.phpfreaks.com/topic/280364-using-part-page-url-as-reference/#findComment-1441555 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. Link to comment https://forums.phpfreaks.com/topic/280364-using-part-page-url-as-reference/#findComment-1441590 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> Link to comment https://forums.phpfreaks.com/topic/280364-using-part-page-url-as-reference/#findComment-1441599 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.