I have a comma separated value like
alpha,beta,charlie
how can I convert it to
"alpha","beta","charlie"
using a single function in php without using str_replace?
Alternatively to Richard Parnaby-King's function (shorter):
function addQuotes($string) {
return '"'. implode('","', explode(',', $string)) .'"';
}
echo addQuotes('alpha,beta,charlie'); // = "alpha","beta","charlie"
what about
<?php
$arr = spliti(",","alpha,beta,charlie");
for($i=0; $i < sizeof($arr); $i++)
$var = $var . '"' . $arr[$i] . '",';
//to avoid comma at the end
$var = substr($var, 0,-1);
echo $var;
?>
with function:
<?php
function AddQuotes($str){
$arr = spliti(",",$str);
for($i=0; $i < sizeof($arr); $i++)
$var = $var . '"' . $arr[$i] . '",';
//to avoid comma at the end
$var = substr($var, 0,-1);
echo $var;
}
AddQuotes("alpha,beta,charlie");
?>
/**
* Take a comma separated string and place double quotes around each value.
* #param String $string comma separated string, eg 'alpha,beta,charlie'
* #return String comma separated, quoted values, eg '"alpha","beta","charlie"'
*/
function addQuote($string)
{
$array = explode(',', $string);
$newArray = array();
foreach($array as $value)
{
$newArray[] = '"' . $value . '"';
}
$newString = implode(',', $newArray);
return $newString;
}
echo addQuote('alpha,beta,charlie'); // results in: "alpha","beta","charlie"
Related
Could somebody explaind me ***why array count is not 2
$value = '"305", "112", ';
//remove last comma and space
echo $value = rtrim($value, ', ');
echo '<br>';
$value = array($value);
echo $array_length = count($value); //***
You should use explode function to get array like the following code :
$value = '"305", "112"';
$value = rtrim($value, ', ');
echo '<br>';
$value = explode(',',$value);
echo $array_length = count($value);
you can use explode() to get it.
$value = '"305", "112", ';
//remove last comma and space
echo $value = rtrim($value, ', ');
echo '<br>';
$value = explode(',',$value);
echo $array_length = count($value);
you can also do this if you want to create an array from that these type of strings and do array count.
$arr = array_filter(array_map('trim', str_getcsv($value)));
print_r($arr);
echo count($arr);
when you are creating an array with array($value), the entire value of $value counted as a single element of an array. Thats why you are getting count($value) not equal to 2.
I have a variable
<?php
$srch_key = 'asdfggfdsa' ;
?>
Now I want to make a separation with comma after 5 letters. For this I have done this code.
<?php
function ref_format($str, $step, $reverse = false) {
if ($reverse)
return strrev(chunk_split(strrev($str), $step, ','));
return chunk_split($str, $step, ',');
}
$passport = ref_format("$srch_key", 5);
echo $passport_key = substr($passport, 0, -1);
?>
The Output seems like this
asdfg,gfdsa
But I want to make the output like this
'asdfg','gfdsa'
How can I make this.
$srch_key = 'asdfggfdsa' ;
$arr = str_split($srch_key, "5");
$res = "'" . implode ( "', '", $arr ) . "'";
echo $res;
You could do this using implode() and str_split():
function ref_format($str, $step, $reverse = false)
{
if ($reverse)
return strrev("'" . implode("','", str_split($str, $step)) . "'");
return "'" . implode("','", str_split($str, $step)) . "'";
}
I have a string that looks like this:
$str = "Col1, Col2, Col3";
My question is how can I make it look like this
FORMAT(SUM('Col1'),2),FORMAT(SUM('Col2'),2),FORMAT(SUM('Col3'),2)
I am trying to use implode and explode but it's not working for me.
Here is my attempt:
$sample = "Col1, Col2, Col3";
$test = explode(",", $sample);
$test = "'" . implode("', FORMAT(SUM('", $test) . "), 2";
$sample = "Col1,Col2,Col3";
$test= explode(',',$sample);
$_test = '';
foreach($test as $t){
$_test .= "FORMAT(SUM('$t'),2),";
}
$_test = rtrim($_test,',');
I dont know if you can achiev this using explode, but you for sure can using a foreach loop.
$sample = 'Col1,Col2,Col3';
$result = '';
$parts = explode(',', $sample);
foreach ($parts as $part) {
$result .= 'FORMAT(SUM(' . $part . '), 2)';
$result .= (end($parts) !== $part) ? ', ' : '';
}
This runs over each part of the exploded array and adds the desired string plus a comma if its not the last element.
You can also use array_walk to achieve the requested result:
$sample = 'Col1,Col2,Col3';
$parts = explode(',', $sample);
$str = '';
array_walk($parts, function ($element) use (&$str) {
$str .= 'FORMAT(SUM(' . $element . '), 2), ';
});
$result = rtrim($str, ', ');
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;
}
$updateRoles=array();
$updateRoles["role_name"]="Manager";
$updateRoles["role_description"]="Manages system";
$whereRoles["roleid"]=15;
$sdb->dbUpdate("user_roles",$updateRoles,$whereRoles);
Look at the sample. I got this code snippet from a library. I'm curious, How can I code
dbUpdate function ? Because this function has dynamic parameters. Can you show me a sample regarding functions with dynamic array parameters?
Look at func_num_args(), func_get_arg() and func_get_args().
http://php.net/manual/en/functions.arguments.php
I solved the problem.
Solution:
function BilgiEkle ($tablo,$data)
{
$tablo = trim($tablo);
$str = "(";
$str2 = "(";
foreach ($data as $key => $value) {
$str = $str . '`' . $key . '`' . ',';
$str2 = $str2 ."'". $value ."'". ',';
}
$str = substr($str, 0, -1);
$str2 = substr($str2, 0, -1);
$str = $str . ")";
$str2 = $str2 . ")";
$sqlifade = "INSERT INTO `$tablo` $str VALUES $str2 ";
$islem=$this->baglan->prepare($sqlifade);
$islem->execute();
return $islem->rowCount();
}
Ex:
$arr=array();
$arr['name']='burhan';
$arr['familyname']='tanis';
bilgiEkle('table1',$arr);