1

Hello Stack Overflow Experts. I found this post and the author states, "... function knows how to escape special characters". I am still learning and I'm having difficulty seeing where the actual "handling" of special characters takes place and how they are "handled." Could someone point me in the right direction? Thank you. Here is the listed code:

$dataArray = csvstring_to_array( file_get_contents('Address.csv'));

function csvstring_to_array($string, $separatorChar = ',', $enclosureChar = '"', $newlineChar = "\n") {
    // @author: Klemen Nagode
    $array = array();
    $size = strlen($string);
    $columnIndex = 0;
    $rowIndex = 0;
    $fieldValue="";
    $isEnclosured = false;
    for($i=0; $i<$size;$i++) {

        $char = $string{$i};
        $addChar = "";

        if($isEnclosured) {
            if($char==$enclosureChar) {

                if($i+1<$size && $string{$i+1}==$enclosureChar){
                    // escaped char
                    $addChar=$char;
                    $i++; // dont check next char
                }else{
                    $isEnclosured = false;
                }
            }else {
                $addChar=$char;
            }
        }else {
            if($char==$enclosureChar) {
                $isEnclosured = true;
            }else {

                if($char==$separatorChar) {

                    $array[$rowIndex][$columnIndex] = $fieldValue;
                    $fieldValue="";

                    $columnIndex++;
                }elseif($char==$newlineChar) {
                    echo $char;
                    $array[$rowIndex][$columnIndex] = $fieldValue;
                    $fieldValue="";
                    $columnIndex=0;
                    $rowIndex++;
                }else {
                    $addChar=$char;
                }
            }
        }
        if($addChar!=""){
            $fieldValue.=$addChar;

        }
    }

    if($fieldValue) { // save last field
        $array[$rowIndex][$columnIndex] = $fieldValue;
    }
    return $array;
}
  • 1
    That statement is extremely vague, so there is no right or wrong here. What the author probably means is that the function tries to handled quoted substrings. That dies not mean that it implements some magic form to handle everything someone might consider a "special character". Which is a questionable term anyway. You can clearly see how this function detect quote chars and maintains some simple mode while iterating through the strings characters. But that is neither elegant nor robust in my eyes. – arkascha Apr 02 '21 at 13:04
  • Why not ask the author? – Markus Zeller Apr 02 '21 at 13:06
  • @Markus Zeller I thought of contacting author but it was 11 years, 7 months ago when this was posted so I thought posting a question might provide more current thinking (including better ways to do this). I will take your advice and give it a try ... – Neil_Tinkerer Apr 02 '21 at 13:11
  • @arkascha That is what I was seeing as well. The quoted substrings was easy to see in the code but I could not see how a character - for example, an apostrophe in a name (O'Brian) would be "handled." In the code it also stated (commented) " // escaped char" but that didn't add any clarity - at least for me. Thank you for providing your thoughts. Much appreciated. – Neil_Tinkerer Apr 02 '21 at 13:15
  • 1
    The variable names and the comment confuse two terms: enclose and escape. That comment only means that the character right before a closing "enclosing character" (so the one before the closing quote character) is preserved. That has nothing to do with actual "escaping". But escaping is not required here actually. Characters like an apostrophe are often referred to as "special", but that is nonsense. They are perfectly normal characters. Issues only arise when some characters have a special meaning, but that always completely depends on the actual environment. – arkascha Apr 02 '21 at 13:26
  • 1
    @arkascha Great lessons here. Thank you. – Neil_Tinkerer Apr 02 '21 at 13:31

0 Answers0