将PHP输出括在引号中

时间:2021-09-19 06:19:16

we have a PHP script that exports orders to .csv files. The system we are exporting too requires each field to be encapsulated in quote marks.

我们有一个PHP脚本,可以将订单导出到.csv文件。我们导出的系统也要求将每个字段封装在引号中。

Here is the code where we set each field.

这是我们设置每个字段的代码。

$order_data = array(
        'type'                => "H",
        'order_type'            => 'HOME',
        'order_date'          => $order->order_date,
        'account_code'          => "REAL", 
        'document_reference'    =>'',
        'reference1'=>'',
        'reference2'=>'',
        'delivery_addess_code'=> '',
        'billing_first_name'  => $order->billing_first_name ." ".$order->billing_last_name,
        'billing_address_1'   => $order->billing_address_1 ." ".$order->billing_address_2,
        'billing_postcode'    => $order->billing_postcode,
        'delivery_tel_no'=>   $order->billing_phone,
        'delivery_contact'=>   $order->billing_first_name,

This outputs;

H,HOME,"2015-05-13 13:19:46",REAL,,,,,"Ben Bull","Address 1 Address2",

H,HOME,“2015-05-13 13:19:46”,REAL ,,,,,“Ben Bull”,“Address 1 Address2”,

Some are surround by "" and some aren't how do we get them all to be?

有些是围绕“”,有些不是我们如何得到它们?

2 个解决方案

#1


For CSV output, you need to enclose all the values with double quotes. In addition, if the values have double quotes inside them you need to escape those double quotes by using two consecutive double quotes. That's how CSV works.

对于CSV输出,您需要用双引号括起所有值。此外,如果值中有双引号,则需要使用两个连续的双引号来转义这些双引号。这就是CSV的工作原理。

Check this PHP function below.

检查下面的PHP函数。

function makeCSV($value) {
    //Encloses each token (Before and after)
    $CSV_TOKEN_ENCLOSER = '"';

    //Used to escape the enclosing character if inside the token
    $CSV_TOKEN_ENCLOSER_ESCAPER = '""';

    //Escape the encloser inside the value
    $csv_value = str_replace($CSV_TOKEN_ENCLOSER, $CSV_TOKEN_ENCLOSER_ESCAPER, $value);

    //Enclose the value
    $csv_value .= $CSV_TOKEN_ENCLOSER . $csv_value . $CSV_TOKEN_ENCLOSER;

    //Return
    return $csv_value;
}

This does the job as I've explained in the first paragraph. You can use it as such in your case:

这就像我在第一段中解释的那样。你可以在你的情况下使用它:

$order_data = array(
    'type'       => makeCSV("H"),
    'order_type' => makeCSV('HOME'),
    'order_date' => makeCSV($order->order_date),
    ...
);

However, it looks like you have code that's enclosing the values from your order objects within quotes automatically for you. I suggest you avoid that code, replace that with the usage of the makeCSV function presented above, and then finally just use a standard PHP implode call to get your CSV like this:

但是,您似乎拥有的代码会自动将订单对象中的值包含在引号内。我建议你避免使用上面提到的makeCSV函数代码,然后最后使用标准的PHP内爆调用来获取你的CSV:

$comma_separated_csv = implode(",", $order_data);

Hope this helps.

希望这可以帮助。

Cheers.

#2


Try to force all types to string like:

尝试强制所有类型字符串如下:

'order_type' => (string) 'HOME'

'order_type'=>(字符串)'HOME'

#1


For CSV output, you need to enclose all the values with double quotes. In addition, if the values have double quotes inside them you need to escape those double quotes by using two consecutive double quotes. That's how CSV works.

对于CSV输出,您需要用双引号括起所有值。此外,如果值中有双引号,则需要使用两个连续的双引号来转义这些双引号。这就是CSV的工作原理。

Check this PHP function below.

检查下面的PHP函数。

function makeCSV($value) {
    //Encloses each token (Before and after)
    $CSV_TOKEN_ENCLOSER = '"';

    //Used to escape the enclosing character if inside the token
    $CSV_TOKEN_ENCLOSER_ESCAPER = '""';

    //Escape the encloser inside the value
    $csv_value = str_replace($CSV_TOKEN_ENCLOSER, $CSV_TOKEN_ENCLOSER_ESCAPER, $value);

    //Enclose the value
    $csv_value .= $CSV_TOKEN_ENCLOSER . $csv_value . $CSV_TOKEN_ENCLOSER;

    //Return
    return $csv_value;
}

This does the job as I've explained in the first paragraph. You can use it as such in your case:

这就像我在第一段中解释的那样。你可以在你的情况下使用它:

$order_data = array(
    'type'       => makeCSV("H"),
    'order_type' => makeCSV('HOME'),
    'order_date' => makeCSV($order->order_date),
    ...
);

However, it looks like you have code that's enclosing the values from your order objects within quotes automatically for you. I suggest you avoid that code, replace that with the usage of the makeCSV function presented above, and then finally just use a standard PHP implode call to get your CSV like this:

但是,您似乎拥有的代码会自动将订单对象中的值包含在引号内。我建议你避免使用上面提到的makeCSV函数代码,然后最后使用标准的PHP内爆调用来获取你的CSV:

$comma_separated_csv = implode(",", $order_data);

Hope this helps.

希望这可以帮助。

Cheers.

#2


Try to force all types to string like:

尝试强制所有类型字符串如下:

'order_type' => (string) 'HOME'

'order_type'=>(字符串)'HOME'