简体   繁体   English

php 内爆 (101) 带引号

[英]php implode (101) with quotes

Imploding a simple array内爆一个简单的数组

would look like this看起来像这样

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

and that would return this这将返回这个

 lastname,email,phone

great, so i might do this instead太好了,所以我可以这样做

$array = array('lastname', 'email', 'phone');
$comma_separated = implode("','", $array);
$comma_separated = "'".$comma_separated."'";

and now i have what I want a nice pretty csv string现在我有了我想要的漂亮漂亮的 csv 字符串

 'lastname','email','phone'

is there a better way to do this, it feels to me like there should be an optional parameter for implode am I missing something?有没有更好的方法来做到这一点,我觉得应该有一个可选参数 implode 我错过了什么吗?

$array = array('lastname', 'email', 'phone');


echo "'" . implode("','", $array) . "'";

You could use array_map() :您可以使用array_map()

function add_quotes($str) {
    return sprintf("'%s'", $str);
}

$csv =  implode(',', array_map('add_quotes', $array));

DEMO演示

Also note that there is fputcsv if you want to write to a file.另请注意,如果要写入文件,则有fputcsv

$ids = sprintf("'%s'", implode("','", $ids ) );

No, the way that you're doing it is just fine.不,你这样做的方式很好。 implode() only takes 1-2 parameters (if you just supply an array, it joins the pieces by an empty string). implode()只需要 1-2 个参数(如果你只提供一个数组,它会通过一个空字符串连接各个部分)。

Don't know if it's quicker, but, you could save a line of code with your method:不知道它是否更快,但是,您可以使用您的方法保存一行代码:

From

$array = array('lastname', 'email', 'phone');
$comma_separated = implode("','", $array);
$comma_separated = "'".$comma_separated."'";

To:至:

$array = array('lastname', 'email', 'phone');
$comma_separated = "'".implode("','", $array)."'";

If you want to use loops you can also do:如果你想使用循环,你也可以这样做:

$array = array('lastname', 'email', 'phone');
foreach($array as &$value){
   $value = "'$value'";
}
$comma_separated = implode(",", $array);

Demo: http://codepad.org/O2kB4fRo演示: http://codepad.org/O2kB4fRo

$id = array(2222,3333,4444,5555,6666);
$ids = "'".implode("','",$id)."'";

Or或者

$ids = sprintf("'%s'", implode("','", $id ) );

Alternatively you can create such a function:或者,您可以创建这样的 function:

function implode_with_quotes(array $data)
{
    return sprintf("'%s'", implode("', '", $data));
}

If you want to avoid the fopen/fputcsv sub-systems here's a snippet that builds an escaped CSV string from an associative array....如果你想避免 fopen/fputcsv 子系统,这里有一个片段,它从关联数组中构建一个转义的 CSV 字符串......

$output = '';
foreach ($list as $row) {
  $output .= '"' . implode('", "', array_values($row)) . '"' . "\r\n";
}

Or from a list of objects...或从对象列表...

foreach ($list as $obj) {
  $output .= '"' . implode('", "', array_values((array) $obj)) . '"' . "\r\n";
}

Then you can output the string as desired.然后您可以根据需要 output 字符串。

I think this is what you are trying to do我认为这就是你想要做的

$array = array('lastname', 'email', 'phone');
echo "'" . implode("','", explode(',', $array)) . "'";

Another possible option, depending on what you need the array for:另一种可能的选择,取决于您需要阵列的用途:

$array = array('lastname', 'email', 'phone');
echo json_encode($array);

This will put '[' and ']' around the string, which you may or may not want.这会将 '[' 和 ']' 放在字符串周围,您可能想要也可能不想要。

you can do it this way also你也可以这样做

<?php
$csv= '\'' . join(array('lastname', 'email', 'phone'),'\',').'\'';
echo $csv;
?>

Don't forget to escape your data!不要忘记转义您的数据! If you want to put data in a CSV file without fputcsv function or put it in a SQL query use this code:如果要将数据放入 CSV 文件中而不使用 fputcsv function 或将其放入 SQL 查询中,请使用以下代码:

$comma_separated = "'" . implode("','", array_map('addslashes', $array)) . "'";

This code avoids SQL injection or data fragmentation when input array contains single quotation or backslash characters.当输入数组包含单引号反斜杠字符时,此代码避免了 SQL 注入或数据碎片。

If you need to keep the double quotes (maybe to format a json?) you could also:如果您需要保留双引号(也许格式化 json?),您还可以:

$array = ["a", "b", "c"];
implode("\",\"", $array) // "a","b","c"

Another solution is achieved using implode + preg_replace as follows:另一种解决方案是使用 implode + preg_replace 实现的,如下所示:

$a = ['A', 'B', 'C'];
implode(',', preg_replace('/^(.*)$/', '"$1"', $a)); // "A","B","C"
$b = [];
implode(',', preg_replace('/^(.*)$/', '"$1"', $b)); // empty string

As you can see this also saves you from checking if the array is empty.如您所见,这也使您免于检查数组是否为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM