This is my PHP
$val = "";
foreach($data as $v){
$val .= '"' . $v . "\n" . '"';
}
The result is
//this is inside of cell
sample
"sample
"sample
This is correct but I cant figure out how to remove the double quote in "sample. Only the first value dont have a double quote the rest will have a double quote.
EDIT
This is the result of my code
This is what I want
Just Remove some part of your code, you will get solution:
$val = "";
foreach($data as $v){
$val .= $v . "\n";
}
Just try (Maintaining line break in excel):
$val = "";
foreach($data as $v){
$val .= $v . "<br style='mso-data-placement:same-cell;' />";
}
Somthing like this should do the trick
$i = 0
foreach($data as $v){
$val .= '"' . $v . "\n" . (count($data) !== $i) ? '"' : '';
$i++;
}
I think you can use the php function trim like this:
$val = "";
foreach($data as $v){
if( ''==$val) {
$val .= trim('"' . $v . "\n" . '"','"');
}else{
$val .= '"' . $v . "\n" . '"','"';
}
}
$val .= '"' . $v . "</br>" . '"';
$val=str_replace('"','',$val);
I think this is what you are looking for inside your loop. It will remove every double quote.
Well there you go :
$data = ['sample','sample','sample'];
$val = "";
foreach($data as $v){
$val .= $v . "\n" ;
}
Then you simply use fputcsv() like this :
$fp = fopen('file.csv', 'w');
fputcsv($fp, [$val]);
And you get the result you want :
The answer to my question is
$val = "";
foreach($data as $v){
$val .= $v . "\n";
}
$val = '"'. $val .'"';
I need to add the '"' outside of the loop.
Related
I have created a form,I read from a INI file and show its values in the form.
Now I want to edit that file.
So I show values in text box and let the user edit the file.
Now I am using INI Library to write my files, but my code changes the structure of INI file, also it remove the comments.
Is it possible that when I write to the file only those value is changed and other things like comments and Section remain as it is ?
This is my code:
$CFG_file = $this->data['file_data'] = parse_ini_file("CFG/fms.cfg" ,true);
if (isset($_POST['submit']))
{
//var_dump($_POST);
$data_file = $this->parameter_m->array_from_post(array('VAL1','VAL2','InMedia','VAL','VAL','VAL5','VAL3'));
$this->load->helper('file');
$file = "CFG/fms.cfg";
$this->load->library('ini');
$ini = new INI($file);
$ini->write($file, $data_file );
}
And the library code that writes file :
function write($file = NULL, $file_content = array(), $sections = FALSE) {
$this->file_content = (!empty($file_content)) ? $file_content : $this->file_content;
$this->file = ($file) ? $file : $this->file;
$this->sections = $sections;
$content = NULL;
if ($this->sections) {
foreach ($this->file_content as $section => $file_content) {
$content .= '[' . $section . ']' . PHP_EOL;
foreach ($file_content as $key => $val) {
if (is_array($val)) {
foreach ($val as $v) {
$content .= $key . '[]=' . (is_numeric($v) ? $v : $v ) . PHP_EOL;
}
} elseif (empty($val)) {
$content .= $key . '=' . PHP_EOL;
} else {
$content .= $key . '=' . (is_numeric($val) ? $val : $val ) . PHP_EOL;
}
}
$content .= PHP_EOL;
}
} else {
foreach ($this->file_content as $key => $val) {
if (is_array($val)) {
foreach ($val as $v) {
$content .= $key . '[] = ' . (is_numeric($v) ? $v : '"' . $v . '"') . PHP_EOL;
}
} elseif (empty($val)) {
$content .= $key . ' = ' . PHP_EOL;
} else {
$content .= $key . ' = ' . (is_numeric($val) ? $val : '"' . $val . '"') . PHP_EOL;
}
}
}
return (($handle = fopen($this->file, 'w+')) && fwrite($handle, trim($content)) && fclose($handle)) ? TRUE : FALSE;
}
And this is my file :
[]
VAL1= 0.0.0.0
VAL2= 5070
;media settings
;ON/OFF/AUTO
VAL3= OFF
VAL4= 8000
VAL5= 4000
VAL6= 360
;Transport settings
;ON/OFF
VAL7= OFF
When i execute my code this becomes :
VAL1= "0.0.0.0"
VAL2 = 5071
VAL3 =
VAL4 = 8000
VAL5 = 4000
VAL6 = 360
VAL7 = "ON"
I have 70 text fields in my HTML form. I want to insert data from those fields to the database using php and mysqli. So I thought of using the foreach loop while creating the sql query..I tried the following, but I am not able to get the required result.
foreach ($_POST as $key => $value) {
$f = $f . "," . $key ;
$v = $v . ",'" . $value . "'";
}
$sql = "insert into table1($f) values ($v)";
The variablefis supposed to carry a string of comma seperated field names which are fields of the $_POST array. While variable v is to carry single quoted comma seperated values of the $_POST array. I am getting an extra comma in the starting of f and v right now. How to remove that.
Please help!
you should use rtrim() to remove that extra commas from the right side
foreach ($_POST as $key => $value) {
$f = $f . "," . $key ;
$v = $v . ",'" . $value . "'";
}
$f = rtrim($f,',');
$v = rtrim($v,',');
$sql = "insert into table1($f) values ($v)";
rtrim($f, ",") would cut trailing commas.
trim($f, ",") would cut trailing and prefixing commas.
you can also use substr() to remove last character from string like below
substr($f, 0, -1);
substr($v, 0, -1);
further reading for
trim() : http://php.net/trim
rtrim() : http://php.net/rtrim
substr() : http://php.net/substr
EDIT
A better way would be
$f = array(); // create blank arrays
$v = array();
foreach ($_POST as $key => $value)
{
$f[] = $key; // push values to the array
$v[] = $value;
}
$f1 = implode(",", $f); // convert array to comma separated string
$v1 = implode(",", $v);
$sql = "insert into table1($f1) values ($v1)";
let me know if that helped you..
Try this
foreach ($_POST as $key => $value) {
$f .= $f . "," . $key ;
$v .= $v . ",'" . $value . "'";
}
$f = ltrim($f,',');
$v = ltrim($v,',');
$sql = "insert into table1($f) values ($v)";
Better to check first using isset() before prefixing comma, this will also remove the warnings you are getting.
foreach($_POST as $key => $value) {
$f = isset($f) ? $f . "," . $key : $key;
$v = isset($v) ? $v . "," . $value : $value;
}
I looking for a way (regex, snippet ,plugin etc) to convert the old arrays with the new php syntax with sublimeText.
// Old synthax
$var = array(
'foo' => 'bar'
);
// New synthax
$var = [
'foo' => 'bar'
];
someone has an idea ?
I found a script that does the job perfectly !
https://github.com/thomasbachem/php-short-array-syntax-converter
I found out that it is also possible to do this with the php codesniffer: https://github.com/squizlabs/PHP_CodeSniffer
phpcbf src/ --standard=Generic --sniffs=Generic.Arrays.DisallowLongArraySyntax
In this example you have to replace src/ with the folder containing the scripts. Alternatively you can provide a file name.
Maybe a bit late, but I created my own. Maybe not pretty, but it does do the job I want. If you do not like tab, change \t in indent function to 2 or 4 spaces.
function loopArray(array $array, $loopcount = 0) {
$returnString = ($loopcount == 0) ? "[\n" : "";
$tabKey = indent($loopcount + 2);
$tabValue = indent($loopcount + 3);
$lastKey = array_key_last($array);
foreach ($array as $key => $value) {
$totalChildren = count($array[$key]);
$returnString .= $tabKey . '"' . $key . '" => ';
if ($totalChildren == 0) $returnString .= '[]';
if ($totalChildren > 0 && is_array($array[$key])) $returnString .= '[' . "\n";
if (is_array($value)) {
$returnString .= loopArray($value, $loopcount + 1);
} else {
if ($totalChildren == 1) $returnString .= '"' . $value . '"';
if ($totalChildren > 1) $returnString .= $tabValue . '"' . $value . '"' . ",\n";
}
$returnString .= ($lastKey == $key) ? "\n" . indent($loopcount+1) . "]" : ",\n";
}
return $returnString;
}
function indent($amount) {
return str_repeat("\t", $amount);
}
/** use function below only prior to php 7.3 */
function array_key_last(array $array) {
$key = NULL;
if ( is_array( $array ) ) {
end( $array );
$key = key( $array );
}
return $key;
}
I'm using var_export to dump output to logs when errors occur. However since the result is in pure text, I don't get a chance to push it through some sort of library like krumo so I can interactively explores the output.
What methods do people have to deal with making var_export text more readable?
Here is my function, it works well for multidimensional arrays:
function VE($varname, $varval, $short_syntax=true, $tag = ' ', $comma='', $end_line="\r\n") {
$res = '';
if($short_syntax){
$begin_array = '[';
$end_array = ']';
} else {
$begin_array = 'array(';
$end_array = ')';
}
$arr = explode('/',$varname);
$dim =count($arr)-1;
$lastKey = end($arr);
if (! is_array($varval)){
if( is_string($varval)) $varval = "'$varval'";
$res .= str_repeat($tag,$dim) . $lastKey . ' => ' . $varval . $comma . $end_line;
}else{
$res .= str_repeat($tag,$dim) . $lastKey . ' => ' . $begin_array . $end_line;
$count_varval = 0;
$dim_varval = count($varval);
foreach ($varval as $key => $val){
$count_varval++;
if($count_varval<$dim_varval) $commma=','; else $commma='';
if( is_string($key)) $key = "'$key'";
$res .= VE ($varname . "/" . $key , $val, $short_syntax, $tag, $commma);
}
$res .= str_repeat($tag,$dim) . $end_array . $comma . $end_line;
}
return $res;
}
$bigarray = array(); // your array
$bb = VE ('$bigarray', $bigarray);
echo "<pre>$bb</pre>";
I hope it helps ;)
$arr = array('key1'=>'val1','key2'=>'val2');
$str = '';
foreach($arr as $k=>$v)
{
$str .= '&' . $k . '=' . urlencode($v);
}
echo substr($str,1);
Or
$arr = array('key1'=>'val1','key2'=>'val2');
$str = '';
foreach($arr as $k=>$v)
{
$str .= '&' . urlencode($k) . '=' . urlencode($v);
}
echo substr($str,1);
Does the $k need to be urlencoded?
Yes;
If you use PHP5 i Guess the http_build_query would be just what you need ;)
http://nl2.php.net/manual/en/function.http-build-query.php