Skip to content

[FIX] function.smart_display_field.php #21

Open

Description

@stfour

Hi Benkeen, may I propose these changes to .php in the subject? This fix csv output if there are textarea fields:

function smarty_function_smart_display_field($params, &$smarty)
{
    $value = FieldTypes::generateViewableField($params);

    // additional code for CSV encoding
    if (isset($params["escape"])) {
        if ($params["escape"] == "csv") {
			// Check if we need to enclose field between double quotas
			$needQuote = (strstr($value, ",") || strstr($value, "\n"));
			// trim spaces and other useless char
			$value = trim($value);
			$value = preg_replace("/\"/", "\"\"", $value);
			// replace inner "\n" or "\r" with '|' character, otherwhise csv will be corrupted.
			$value = str_replace(array("\n","\r"), '|', $value);
			// add double quotas if required
			if ($needQuote)
				{
				$value = "\"$value\"";
				}
        }
        if ($params["escape"] == "excel") {
            $value = preg_replace("/(\n\r|\n)/", "<br style=\"mso-data-placement:same-cell;\" />", $value);
        }
    }

    echo $value;
}

Activity

  1. stfour commented on Feb 20, 2019

    @stfour
    Author

    The above code is for US CSV, for IT I need

    $needQuote = (strstr($value, ";") || strstr($value, "\n"));

    so ';' instead of ','... I think we can safely use the following for both:

    $needQuote = (strstr($value, ";") || strstr($value, ",") || strstr($value, "\n"));

    Ciao
    Marco (stfour)

  2. benkeen commented on Feb 21, 2019

    @benkeen
    Member

    Hey @stfour, interesting...! Thanks for posting this.

    Could you clarify exactly the difference between Italian and US? I thought CSV was just a standard - I mean, you can choose different delimiters but I wasn't aware that was language/country specific.

    I wonder if perhaps another approach to solve this would be to add a setting to the Export Manager UI named "Delimiter", and then have everything automatically work based off that?

  3. stfour commented on Feb 21, 2019

    @stfour
    Author

    The problem is the decimal separator. Any country that uses comma ',' as decimal separator (instead of poin '.') will use semicolon ';' in CSV. This is default behaviour of Excel and most of apps that uses Window's default regional settings.

    As you can see we are not alone:
    https://en.wikipedia.org/wiki/Decimal_separator

    As far I know the only standard CSV is comma for column and point as decimal. So, yes, programs that uses semicolon and comma for numbers in CSV are wrong. But in real world, the standard is how Excel open the files.

    Ciao

    Marco

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions