How to print "" in php varible in html file - php

Okay so when I do:
<br>
$line9 = "<button onclick='window.location.href='//home-pc/';'/>GoBack</button>";
$phpfiletxt
"$line1\n$line2\n$line3\n$line4\n$line5\n$line6\n$line7\n$line8\n$line9\n$line10\n$line11\n$line12\n$line13\n$line14\n$line15";<br>
$myfile = fopen("$folder/index.html", "w") or die("Unable to open file!");<br>
fwrite($myfile, $phpfiletxt);<br>
fclose($myfile);<br>
$wordlink = "<a class='list' target='_top' href='$folder'>$word</a>";<br>
$myfile = fopen("wordslist.php", "a") or die("Unable to open file!");<br>
fwrite($myfile, $wordlink);<br>
header("Location: $folder");<br>
where the button is i want to make it work because it doesn't do anything becuase the ' or " arent placed correctly
so i need it so this is a varible that i can print:
onclick='window.location.href='//home-pc/';'/>GoBack</button>";
into an html file. Thanks

In relation to :
How to print "" in php varible in html file
You do as follows:
$string = "The man said\"Hello\" and ran away!";
echo $string;
result: The man said"Hello" and ran away!
It's called escaping. http://php.net/manual/en/language.types.string.php

<button onclick="window.location.href='/home/pc'">GoBack</button>;
Is what you need to replace that with.

Try this
$line9 = "<button onclick=\"window.location.href='//home-pc/';\">GoBack</button>";

I always use ' instead of ", because html has so many " need to be escape like \"...\"
$string = '<a class="text-success" id="success">success></a>'

Related

PHP statement within echo

I am essentially trying to combine to PHP statements.
<?php echo ROOT_PATH; ?>
<?php echo file_get_contents( "../css/themes/subtitle.php"); ?>
I want to achieve something like this:
<?php echo file_get_contents( "ROOT_PATH/css/themes/subtitle.php"); ?>
If you call constants inside single or double quotes then it will be always picked as a string.
You need to add like below:
<?php echo file_get_contents( ROOT_PATH."/css/themes/subtitle.php"); ?>
Its pretty simple you can change it to following
//full path of the file
$file_name = ROOT_PATH."/css/themes/subtitle.php";
echo file_get_contents($file_name);
it should work
. operator is used to concatenating.
Like:
$str = "Hello";
echo $str;
echo "World";
can be written as
$str = "Hello";
echo $str."World";

getting php as text on output

first of all this is my php code. i am trying to get this code as text on output. not sure how i do that. with html we can do \" to insert it inside php but how do i get this done like that on my code ?
<?php
$stringData = "
// Start here
<?php
$width = $_GET['width'];
$heigh = $_GET['height'];
echo 'Hello';
?>
// End here
";
?>
i have marked that part that i want to get it on output as text but when i put it like that on page i get syntax error not sure why.
EDIT #2
here below is my full code and i explain how my code works and for what.
my code is to create page and put something inside that page created
<form method="post">
<label>Page Name:</label><br>
<input type='text' name='filename' placeholder='page name'>
<label>Folders</label>
<select name="thisfolder">
<option value="">Default</option>
<option value="Folder1">Folder1</option>
<option value="Folder2">Folder2</option>
<option value="Folder3">Folder3</option>
</select><br><br>
<label>content</label><br>
<input type='text' name='strin' placeholder='content of created page'>
<input type='submit' value='Add Feed'>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// the name of the file to create
$filename=$_POST['filename'];
// the name of the file to be in page created
$strin=$_POST['strin'];
// the name of the folder to put $filename in
$thisFolder = $_POST['thisfolder'];
// make sure #thisFolder of actually a folder
if (!is_dir(__DIR__.'/'.$thisFolder)) {
// if not, we need to make a new folder
mkdir(__DIR__.'/'.$thisFolder);
}
// . . . /[folder name]/page[file name].php
$myFile = __DIR__.'/'.$thisFolder. "/page" .$filename.".php";
$fh = fopen($myFile, 'w');
$stringData = "
<?php
$width = $_GET['width'];
$heigh = $_GET['height'];
echo '';
?>
";
fwrite($fh, $stringData);
fclose($fh);
}
?>
what i am trying to do is, passing that php code that is inside $stringData to that page that will be created
You'll want to escape the $'s in your text.
Set $stringData like this:
$stringData = "
// Start here
<?php
\$width = \$_GET['width'];
\$heigh = \$_GET['height'];
echo 'Hello';
?>
// End here
";
using highlight_string internal function
echo highlight_string($stringData);
or using htmlspecialchars
echo htmlspecialchars($stringData);
EDIT , as long as you don't want to print the php code literally to the output [as you've mentioned in your comment]
the problem here is that you are using (double quotes) to store values, which has special meaning in php
the solution is to store your text in single quotes ,
<?php
$stringData = '
// Start here
<?php
$width = $_GET["width"];
$heigh = $_GET["height"];
echo "Hello";
?>
// End here
';
?>
You're using double quotes (") which lets you use $variables inside the string where single quotes (') will not.
like so:
$color = 'red';
$string_one = "My car is $color."
echo $string_one; // My car is red.
$string_two = 'My car is $color.'
echo $string_two; // My car is $color.
So to fix your code you simply need to change the double quotes to single quotes (and escape [put a backslash before] the other single quotes).
Like so:
<?php
$stringData = '
// Start here
<?php
$width = \$_GET[\'width\'];
$heigh = \$_GET[\'height\'];
echo \'Hello\';
?>
// End here
';
?>
In your code I added:
<?php
$stringData = '
// Start here
<?php
$width = $_GET["width"];
$heigh = $_GET["height"];
echo "Hello";
?>
// End here
';
echo $stringData;
?>
When I opened this phap page, I had in browser:
// Start here // End here
In Page Source View I had:
// Start here
<?php
$width = $_GET["width"];
$heigh = $_GET["height"];
echo "Hello";
?>
// End here
There is no error! I see now what you wont.
This code with "(string) $price" working:
<?php
$price = 10;
$stringData = "start here (string) $price end here";
echo $stringData;
echo "(string) $price";
?>
Your code
?>
// End here must be in out put
";
?>
There is not start php delimiter <?php
Correct is:
?>
// End here must be in out put
<?php
";
?>
You missed one more php delimiter, total 2:
<?php
$stringData = "
**?>**
// Start here must be in out put
<?php
$width = $_GET['width'];
$heigh = $_GET['height'];
echo '';
?>
// End here must be in out put
**<?php**
";
?>

how to echo php file without pars?

how to read a php file and echo it in a html file?
i try to use readfile() , file() file_get_content() to read a php file.
but when i echo file_string its parsed and then show.
how i can prevent to pars stirng var that included php codes.
here my code:
<?php
$path = '..../ex.php';
$source = fopen($path , "r");
echo fread($source,filesize($path ));
fclose($source);
?>
how to echo $source without compiled or parsed.
With this function
<?php
echo htmlspecialchars($text);
?>
php.net/htmlspecialchars
fread should works fine. Remember that when you use echo it prints <?php opening tag and in rendered page it can be not visible.
To test it, just try with var_dump:
$content = fread($source,filesize($path));
var_dump($content);
I'll go out on a limb and guess that the code is not showing up completely in your HTML page, because the browser is trying to interpret <?php as HTML tags. The solution is to HTML encode any text which may contain characters with a special meaning in HTML:
echo htmlspecialchars(file_get_contents('..../ex.php'));
See The Great Escapism (Or: What You Need To Know To Work With Text Within Text).
use this
$path = 'ex.php';
$source = fopen($path , "r");
echo "<textarea style='border:0px; overflow: hidden; width:100%; height:100% '>";
echo fread($source,filesize($path ));
echo "</textarea>";
fclose($source);

Jquery send data within function param

I have this issue with my jquery code:
<?php
//I oppen a text document, read the text inside of it and write it inside my html page.
//When someone clicks on a line, I want to take that very same line and send it via select(String) function.
$handle = fopen($_POST['lien'], 'r');
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
echo "<div onclick='select(\" ".$buffer." \");'>".$buffer."</div><br/>";
//It works when I put a simple string within the select param:
//echo "<div onclick='select(\" text \");'>".$buffer."</div><br/>";
}
fclose($handle);
}
?>
The jquery code :
function select(text){
alert(text);
//$("#selected").html();
}
where do you guys think is the problem ?
Thanks :)
Maybe Your $buffer contains some double-quotes, which then interfere with surrounding double-quotes. Look at produced HTML code if this is the case, maybe You'll see something like this:
<div onclick='select("He said: "Freeze!"")'>
... which Javascript can't parse correctly. If this is the case, consider using:
echo "<div onclick='select(".json_encode($buffer).")'>";
Try to change line:
echo "<div onclick='select(\" ".$buffer." \");'>".$buffer."</div><br/>";
to
echo "<div onclick='select(\" ".rtrim($buffer)." \");'>".$buffer."</div><br/>";
or to:
echo "<div onclick='select($(this).text());'>".$buffer."</div><br/>";

PHP, Line break when writting to a file

I am trying to keep a running total of all the responses to a form I have written, but I am having trouble making it so that each response takes a new line. I have my code down below. I just want it so that it is easier to read because right now what happens is that all the responses are jammed together would like to have each one on a new line. I tried a few things and have them commented in the code and what the result was. Thanks in Advance.
<?php
if (isset($_POST['sometext']))
{
$myFile = "testFile.txt";
$thetext=$_POST['sometext'] ;//added + "\n" here but all response turned to 0
writemyfile($myFile,$thetext,"a");
} else
{
$thetext="Enter text here";
}
function readmyfile($thefile)
{
$file = fopen($thefile, "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
}
function writemyfile($thefilename,$data,$mode)
{
$myfile=fopen($thefilename,$mode);
fwrite($myfile, $data); // added + "\n" here and responses turned 0
fclose($myfile);
}
?>
<html>
<head>
<title> Zain's Test Site</title></head>
<body>
<form method="post" action="<?php echo $php_self ?>">
<input type="text" name="sometext" value="<?php echo $thetext ?>" >
<input type="submit" name="Submit" value="Click this button">
</form>
<?php readmyfile("testFile.txt"); ?>
</body>
Can you try appending the newline character (\n) to the $thetext variable like this:
$thetext=$_POST['sometext'] . "\n";
Remember to use '.' as the concatenation operator, and use double-quotes around the newline character.
$thetext."\n"
in php you concatenate strings using ".", you use "+" in javascript.
Use newline "\n" instead of the br's which is for html
$text = $text."\n" ?
Err here's some more text to fill out the answer
fwrite($myfile, $data); // added + "\n" here and responses turned 0
the concat string operator is (.) not (+)
you can also simplify your script thusly
echo nl2br(get_file_contents($file));

Categories