Page 1 of 1
Quotation marks in shell_exec("echo...
Posted: Sun Jun 08, 2008 3:56 am
by wilsonee
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.
Re: Quotation marks in shell_exec("echo...
Posted: Sun Jun 08, 2008 9:48 am
by dbemowsk
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...
Posted: Sun Jun 08, 2008 10:29 am
by wilsonee
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.
Re: Quotation marks in shell_exec("echo...
Posted: Sun Jun 08, 2008 10:35 am
by dbemowsk
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
Re: Quotation marks in shell_exec("echo...
Posted: Sun Jun 08, 2008 10:46 am
by wilsonee
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.
Re: Quotation marks in shell_exec("echo...
Posted: Sun Jun 08, 2008 10:55 am
by wilsonee
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);
?>
Re: Quotation marks in shell_exec("echo...
Posted: Sun Jun 08, 2008 12:27 pm
by dbemowsk
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
Code: Select all
$memsize = $_POST['memsize'];
$nvram = $_POST['nvram'];
$formarray = array("memsize => $memsize", "nvram => $nvram");
to this
Code: Select all
$formarray = array("memsize" => $_POST['memsize'];, "nvram" => $_POST['nvram']);
No point in wasting memory. Besides, that is shorter anyway.
Another thing, with this code:
Code: Select all
foreach ($formarray as $k => $v) {
$string = $formarray[$k];
list($item, $vmvalue) = split(' => ',$string);
The way I have rewritten the $formarray above would then change the code to this:
Code: Select all
foreach ($formarray as $item => $vmvalue) {
These changes make the code a bit less cumbersome.