I have already search about this question,I want to remove line break from the html,but there a some split php line in the html file,for example:
<html>
<head>
<title><?='abc'?></title>
</head>
<?php
if($_SESSION['test'] == 'yes'){
echo 'hello';
}
?>
123456
43567
<?='13245tryt57u68'?>
</body>
</html>
how can I remove line break from this php file?
You could use ob_start();
<?php ob_start();
//Start page output
?>
<html>
<head>
<title><?='abc';?></title>
</head>
<body>
<?php
if(isset($_SESSION['test']) && $_SESSION['test'] == 'yes'){
echo 'hello';
}
?>
123456
43567
<?='13245tryt57u68';?>
</body>
</html>
<?php
//End page output and assign the contents to a variable
$buffer = ob_get_contents();
ob_end_clean();
//Replace the new line with null
echo str_replace("\n",null,$buffer);
?>
Result:
<html><head><title>abc</title></head><body>1234564356713245tryt57u68</body></html>
If you want it in your IDE:
Use replace function, and replace \n by no character
If you want it for the client:
You have to configure your htaccess to remove the lines breaks/indenting when sending the file to the client, ie by using the mod_pagespeed extension:
http://www.the-art-of-web.com/system/mod-pagespeed-settings/ and the collapse_whitespace option
Related
I am using simple-html-dom for my work. I want to get all PHP script (<?php ... ?>) form file using simple-html-dom.
if i have one file (name: text.php) with below code :
<html>
<head>
<title>Title</title>
</head>
<body>
<?php echo "This is test Text"; ?>
</body>
</html>
then how can i get this PHP script <?php echo "This is test Text"; ?> form above file of code using simple-html-dom.
$html = file_get_html('text.php');
foreach($html->find('<?php') as $element) {
//Sonthing code ...
}
i can not use like this, Is there any other option for this ?
Here's a solution using regex. Note that regex often is not advisable for parsing HTML files. That is, it might be okay in this case.
This will match each instance of a PHP code block and allow you to output (or do whatever else you want) either the entire block (including the tags) or the code that is contained within the block. See the documentation for preg_match_all().
<?php
$string = <<<'NOW'
<html>
<head>
<title>Title</title>
<?php echo "something else"; ?>
</head>
<body>
<?php echo "This is test Text"; ?>
</body>
</html>
NOW;
preg_match_all("/\<\?php (.*) \?\>/", $string, $matches);
foreach($matches[0] as $index => $phpBlock)
{
echo "Full block: " . $phpBlock;
echo "\n\n";
echo "Command: " . $matches[1][$index];
echo "\n\n";
}
DEMO
When I try to open js.php, it gives an error in xampp while in 000webhost it doesn't . I don't understand why.
so here is the code.
js.php
<?php
header('Content: text/javascript');
if($_SESSION['myjskey'] != hash('md5','examplekey')){
die('No Auth');
}
$_SESSION['JSSESSID'] = 'change';//so that no one can access directly
?>
//secret js
alert('javascript done.');
//and some more js
index.php
<?php
session_start();
$_SESSION['myjskey'] = hash('md5','examplekey');
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP</title>
</head>
<body>
this is a test.
<script type="text/javascript"><?php include('js.php'); ?></script>
</body>
</html>
Website js.php
Xampp Js.php
Edit 1: I cannot do session_start(); in js.php as it will give an error index.php
(session already started.) Removing the header didn't work.
<?php
//js
if($_SESSION['myjskey'] != hash('md5','examplekey')){
die('No Auth');
}
$_SESSION['JSSESSID'] = 'change';//so that no one can access directly
?>
//secret js
alert('javascript done.');
//and some more js
make this change no need to use
header('Content: text/javascript');
I need to display html source code form other php file.
I have two file
code.php
index.php (I hope I can convert the code.php to html source code.)
code.php:
<!DOCTYPE html> <html> <body> <?php $color = "red"; echo $color; ?> </body> </html>
index.php (I hope I can convert the code.php to html source code.)
$php_to_html = file_get_contents("code.php");
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
but when i run the index.php file, the result is
<!DOCTYPE html> <html> <body> <?php $color = "red"; echo $color; ?> </body> </html>
but I hope I can see the result is
<!DOCTYPE html> <html> <body> red </body> </html>
any idea how can i do this ,thanks!!!
You want to execute the PHP, so include it and capture the output:
ob_start();
include("code.php");
$php_to_html = ob_get_clean();
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
If you want the HTML to be rendered as HTML then don't use htmlentities().
Optionally (not the best way) but you can execute it by retrieving from the URL:
$php_to_html = file_get_contents("http://www.example.com/code.php");
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
Buffer output and include it:
ob_start();
include_once('code.php');
$html = ob_get_clean();
By using output buffering, any output is not sent to the browser, but instead kept in memory. This allows you to run the code, and get the output as a variable. ob_get_clean() flushes the buffer (in this case into our $html variable), and then stops buffering, allowing you to continue as normal. :-)
I am trying to send the information as a mail to the admin
Below is the mail code and I am reading this using file_get_contents() in another file.
<!doctype html>
<?php
require_once("mysqlconnect.php");
session_start();
?>
<html>
<head>
<link rel="stylesheet" href="css/mail.css">
</head>
<body>
<table border="2">
<?php
$q="select * from profile where mobile=2147483647";
$result=mysqli_query($conn,$q);
while($row = mysqli_fetch_assoc($result))
{
foreach($row as $key=>$value)
{
if($key=="Email")
echo"<tr><th>$key</th><td><a href='mailto:$value; target='_blank'>$value</td></tr>";
else
echo"<tr><th>".str_replace('_',' ',$key)."</th> <td>$value</td></tr>";
}
}
?>
</table>
</body>
</html>
<?php
session_destroy();
?>
The result of this code in the mail is the PHP code itself..
$value) { if($key=="Email") echo"
$key $value
"; else echo"
".str_replace('_',' ',$key)." $value
"; } } ?>
file_get_contents returns the content of a file as string. It does not executes the code inside it.
To use the HTML from a php file you can use php's buffer.
Like this:
ob_start();
include('file_path_here');
$HTML = ob_get_clean();
This will store the output of the file in the $HTML variable.
file_get_contents literally reads in a file as a string. If you're reading in a PHP file using that method, it'll literally return the code, which is what you're getting.
If you want to send HTML as an e-mail, use something like a template system (sitecrafting.com/blog/top-5-php-template-engines) or build a string and pass it onto the mail() function.
PHP documentation: file_get_contents
I currently have a php file that I'm using as a template but I need it to read in data from another php file that I'm using for the page content. I'm doing it this way to save on code and time, however it doesn't appear to be working. I have done a test with shorter amounts of code but it still isn't working. they are both .php files.
Code -
<html>
<head>
<title></title>
</head>
<body>
<?php
$temp = 'test-2.php';
$file = fopen($temp, 'r');
$cont = fread($file, filesize($temp));
print $cont;
fclose($file);
?>
</body>
test-2.php
<?php
echo 'hello world';
?>
just use <?php include('test-2.php'); ?> surely that'll do what you want?