How to use a PHP include to print HTML code based on URL filename
I'm trying to write a simple PHP script that uses PHP include to inject HTML based on the filename of the URL.
For example using a PHP include - if the filename in the URL is dog.html print dog into the html document. - if the filename in the URL is cat.html print cat into the html document.
This is to inject JSON-LD code for structured data.
I'm able to use PHP includes to print html code without issue, but its getting it to read the filename and print a specific portion of HTML that I can't seem to make work.
You can use $_SERVER['REQUEST_URI'] to grab the Path, File, and Query of a url such as ( ..dog.php?dep=n/a&title=boss ) from a full url eg. http://www.weeex.com/dog.php?dep=n/a&title=boss
To solve your case:
//Grab the filename from the uri using the basename function. $request_uri = $_SERVER['REQUEST_URI'] ; //I am exploding at the "?" delimiter so that I can get rid of the query I don't need $path=explode("?",$request_uri); $filename=basename($path); //YOUR PHP INCLUDE CODE HERE include $filename ;
this will work to grab the filename even with a complicated url like......"http://www.ex.com/dog.php?dep=n/a&title=boss"
I hope this will shed some light
include - Manual, Files are included based on the file path given or, if none is given, the When a file is included, parsing drops out of PHP mode and into HTML mode at PHP code, variables may be passed to the included file using a URL request Example #6 Using output buffering to include a PHP file into a string include $ filename; The syntax is highlighted by using HTML tags. The colors used for highlighting can be set in the php.ini file or with the ini_set() function. show_source() is an alias of highlight_file(). Note: When using this function, the entire file will be displayed - including passwords and any other sensitive information!
You can get the filename using
__FILE__ constant. It is a pre-built constant in PHP. PHP has some other constant like
file_get_contents - Manual, file_get_contents ( string $filename [, bool $use_include_path = FALSE [ The FILE_USE_INCLUDE_PATH constant can be used to trigger include path search. the guidelines on http://www.w3.org/TR/html401/interact/forms.html#h-17.13. 4.2 If you are using file_get_contents() function to retrieve HTTP url and printing� If "URL include wrappers" are enabled in PHP, you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Supported Protocols and Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL
Well it depends how you are sending a html file to PHP script.? If sent in $_GET, Than get file name simply replace .html using str_replace and print it..
basename - Manual, Returns the base name of the given path . The above example will output: Here is a quick way of fetching only the filename (without extension) There is a real problem when using this function on *nix servers, since it does not http:// softontherocks.blogspot.com/2013/07/obtener-la-extension-de-un-fichero-con. html > PHP echo and print Statements. echo and print are more or less the same. They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument.
PHP basename() Function, echo basename($path) ."<br/>"; //Show filename, but cut off file extension for ". php" files echo basename($path,".php"); ?> The output of the code above will be:. Use PRINT or ECHO . This other way is basically the opposite; it's how you'd add HTML to a PHP file with PRINT or ECHO, where either command is used to simply print HTML on the page. With this method, you can include the HTML inside of the PHP tags.
PHP include and require, The include (or require ) statement takes all the text/code/markup that exists in the Including files is very useful when you want to include the same PHP, HTML, or text on So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. include ' filename'; or For example, PHP does not provide a variable that will return the the base URL of your site. To help out, you can use the following code snippets to get the absolute path, document root, and base URL, respectively.
actually i am using lot of php files in my application, I using some code in all pages, common. like db connection. – user1934179 Jan 3 '13 at 10:20 add a comment | 4 Answers 4
- Please read How to create a Minimal, Reproducible Example and How do I ask a good question? and edit your question accordingly.