Hi,
If I want a string output like the following in out.txt. How do I do this?
hello there "John"
$part1 = "hello there";
$part2 = "John";
$output = shell_exec("echo $part1 $part2 >> out.txt);
I've tried below but doesn't work
$output = shell_exec("echo $part1 \"$part2\" >> out.txt);
Thanks.
Quotation marks in shell_exec("echo...
Moderator: General Moderators
Re: Quotation marks in shell_exec("echo...
Your escaped quotes need to be enclosed in quotes like this.
Code: Select all
$output = shell_exec("echo".$part1."\"".$part2."\" >> out.txt");Re: Quotation marks in shell_exec("echo...
Hi dbemowsk,
I've tried your suggestion, I get an out.txt file with no text (empty), but the strange thing is the size of file indicates there is text, just not seeing the text... What could be the reason?
Could you tell me what article I should look at, or a search keyword would be nice.
Thanks.
I've tried your suggestion, I get an out.txt file with no text (empty), but the strange thing is the size of file indicates there is text, just not seeing the text... What could be the reason?
Could you tell me what article I should look at, or a search keyword would be nice.
Thanks.
Re: Quotation marks in shell_exec("echo...
what size does it say the file is? Should be around 18 bytes or so.
Instead of using a shell_exec command to echo data to a file, you may want to use something like this http://www.phptutorial.info/?fwrite
Instead of using a shell_exec command to echo data to a file, you may want to use something like this http://www.phptutorial.info/?fwrite
Re: Quotation marks in shell_exec("echo...
It's the correct size of text that got entered but not showing in out.txt is a mystery.
I've tried fwrite also but I have some more code that uses fopen within a loop.
Calling another fopen didn't like it I think...I just want to make it as simple as possible, so I started using shell_exec. Do you have any suggestions? I'd really like to use shell_exec if possible.
Thanks.
I've tried fwrite also but I have some more code that uses fopen within a loop.
Calling another fopen didn't like it I think...I just want to make it as simple as possible, so I started using shell_exec. Do you have any suggestions? I'd really like to use shell_exec if possible.
Thanks.
Re: Quotation marks in shell_exec("echo...
This is part of my code, I have to output a text with following format.
xxxx = "yyyy"
I can get xxxx = yyyy but not yyyy within quotes. I don't mind using shell_exec sed, anything to get the output really... Thanks.
<form enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000000" />
<SELECT NAME="memsize">
<OPTION VALUE="4">4 MB</OPTION>
<OPTION VALUE="8">8 MB</OPTION>
<OPTION VALUE="512" SELECTED>512 MB</OPTION>
</SELECT>
<INPUT TYPE="text" NAME="nvram" VALUE="nvram">
<tr>
<td width="100"> </td>
<td><input name="add" type="submit" id="add" value="Submit"></td>
</tr>
<tr>
<?php
if($_POST['add']){
$memsize = $_POST['memsize'];
$nvram = $_POST['nvram'];
$formarray = array("memsize => $memsize", "nvram => $nvram");
//==============================================================
$row = 1;
$handle = fopen("config", "r");
while(($data = fgetcsv($handle, 1000, " = ")) !== FALSE) {
foreach ($formarray as $k => $v) {
$string = $formarray[$k];
list($item, $vmvalue) = split(' => ',$string);
//============================================================
if (strcmp($item, $data[0]) == 0) {
$data[2] = "$vmvalue";
}
}
$part1 = "$data[0] $data[1]";
$part2 = "$data[2]";
$output = shell_exec("echo".$data[0]."\"".$data[2]."\" >> out.txt");
echo "<pre> $output </pre>\n";
echo "<p>$data[0]"."$data[1]"."$data[2]"."<br /></p>";
//=============================================================
}
}
fclose($handle);
?>
xxxx = "yyyy"
I can get xxxx = yyyy but not yyyy within quotes. I don't mind using shell_exec sed, anything to get the output really... Thanks.
<form enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000000" />
<SELECT NAME="memsize">
<OPTION VALUE="4">4 MB</OPTION>
<OPTION VALUE="8">8 MB</OPTION>
<OPTION VALUE="512" SELECTED>512 MB</OPTION>
</SELECT>
<INPUT TYPE="text" NAME="nvram" VALUE="nvram">
<tr>
<td width="100"> </td>
<td><input name="add" type="submit" id="add" value="Submit"></td>
</tr>
<tr>
<?php
if($_POST['add']){
$memsize = $_POST['memsize'];
$nvram = $_POST['nvram'];
$formarray = array("memsize => $memsize", "nvram => $nvram");
//==============================================================
$row = 1;
$handle = fopen("config", "r");
while(($data = fgetcsv($handle, 1000, " = ")) !== FALSE) {
foreach ($formarray as $k => $v) {
$string = $formarray[$k];
list($item, $vmvalue) = split(' => ',$string);
//============================================================
if (strcmp($item, $data[0]) == 0) {
$data[2] = "$vmvalue";
}
}
$part1 = "$data[0] $data[1]";
$part2 = "$data[2]";
$output = shell_exec("echo".$data[0]."\"".$data[2]."\" >> out.txt");
echo "<pre> $output </pre>\n";
echo "<p>$data[0]"."$data[1]"."$data[2]"."<br /></p>";
//=============================================================
}
}
fclose($handle);
?>
Re: Quotation marks in shell_exec("echo...
Please use PHP or Code tags with your code posts.
You need to fclose your config file before you try to open your next file. I would create a function that does the file writing. You can then call the function with the filename and the data you want to add to the file. If you are unsure on how to write a function, here is a little tutorial http://www.tizag.com/phpT/phpfunctions.php. Otherwise you can do a google search on "php function write data to file" and you will probably find someone who has already written a function that you can use.
Also, I would change this code
to this
No point in wasting memory. Besides, that is shorter anyway.
Another thing, with this code:
The way I have rewritten the $formarray above would then change the code to this:
These changes make the code a bit less cumbersome.
You need to fclose your config file before you try to open your next file. I would create a function that does the file writing. You can then call the function with the filename and the data you want to add to the file. If you are unsure on how to write a function, here is a little tutorial http://www.tizag.com/phpT/phpfunctions.php. Otherwise you can do a google search on "php function write data to file" and you will probably find someone who has already written a function that you can use.
Also, I would change this code
Code: Select all
$memsize = $_POST['memsize'];
$nvram = $_POST['nvram'];
$formarray = array("memsize => $memsize", "nvram => $nvram");
Code: Select all
$formarray = array("memsize" => $_POST['memsize'];, "nvram" => $_POST['nvram']);
Another thing, with this code:
Code: Select all
foreach ($formarray as $k => $v) {
$string = $formarray[$k];
list($item, $vmvalue) = split(' => ',$string);
Code: Select all
foreach ($formarray as $item => $vmvalue) {