HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/deensekroon.komma-mediadesign.nl/wwwroot/admin/php/Csv/CsvService.php
<?php


namespace Admin\Csv;


class CsvService
{
    protected $title;

    protected $separator = ';';

    /**
     * @param $title
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }


    public function write(Array $headings, Array $rows)
    {
        $contents = '';

        // Insert a title
        if( ! empty($this->title)) $contents .= $this->title . "\n\n";

        // Loop through columns for header row
        foreach($headings as $key => $heading)
        {
            $heading = $this->prepare($heading);

            if( $key != 0) $contents .= $this->separator;
            $contents .= strtoupper($heading);
        }
        $contents .= "\n\r";

        // Loop through data
        foreach($rows as $key => $row)
        {
            // Typecast to array
            $row = (array) $row;

            // Add row
            foreach($row as $column => $value)
            {
                $value = $this->prepare($value);

                $contents .= '"' . $value . '"';
                $contents .= $this->separator;
            }
            $contents = substr($contents,0,-1);

            // New line
            $contents .= "\n";
        }

        return $contents;
    }


    protected function prepare($value)
    {
        $value = htmlspecialchars_decode($value);
        $value = strip_tags($value);
        $value = trim($value);

        return str_replace($this->separator,'',$value);
    }
}