[php] [html] extract only one line

aidenhera

BANNED
Joined
Nov 30, 2016
Messages
3,059
Reaction score
915
i want to retrieve code from php file in HTML file.

so I use include method.

however i cannot extract only one line. I dont want to create this many php files so is there a way to extract only one line from php file and show it in html file?
 
If I understand what you're saying correctly, something like the below should be able to solve your issue. Split your PHP file into separate functions, include the full file (which would be function definitions rather than HTML that is included directly), and then call the function(s) for the parts that you want to show.

Code:
<?php

function printHeader()
{
?>
   <html>
       <head>
           <title>Test Page</title>
       </head>
       <body>
<?
}

function printFooter()
{
?>
       </body>
   </html>
<? 
}

?>
Source credit: https://stackoverflow.com/questions/2111974/how-to-i-inject-only-certain-parts-of-php-page-using-php

If necessary/relevant, you can also make an extra function like the below to allow for the full file to be loaded.
Code:
<?php

function printAll()
{
  printFooter();
  printHeader();
}

?>

You can also/alternatively do something like this, where you define some variable before including the file. The variable will still be defined when the included file runs, so you can use this to pass information regarding what to include/exclude:
HTML FILE
Code:
<?
$only_include_header = true;
include somefile.php;
?>

PHP FILE
Code:
<?
<div>some header</div>
if (!$only_include_header):
  <div>some body</div>
endif;
?>
 
Back
Top