How to define a separator in a string in PHP?

I am wondering if you have a string, how would you define a separator?

We know that php can break a string using explode (), which requires a separator parameter.

But what about the delimiter detection method before sending it to the break function?

Now I just output the line to the user and enter the delimiter. This is great - but I'm looking for a template application that recognizes for me.

Should I look for regular expressions for this type of pattern recognition in a string?

EDIT: I was unable to initially indicate that there is a likely expected set of delimiters. Any delimiter that is probably used in CSV. Therefore, technically, anyone can use any character to delimit the CSV file, but it is more likely to use one of the following characters: comma, semicolon, vertical bar and space.

EDIT 2: Here is a feasible solution that I came up with for the “specific delimiter”.

$get_images = "86236058.jpg 86236134.jpg 86236134.jpg";

    //Detection of delimiter of image filenames.
        $probable_delimiters = array(",", " ", "|", ";");

        $delimiter_count_array = array(); 

        foreach ($probable_delimiters as $probable_delimiter) {

            $probable_delimiter_count = substr_count($get_images, $probable_delimiter);
            $delimiter_count_array[$probable_delimiter] = $probable_delimiter_count;

        }

        $max_value = max($delimiter_count_array);
        $determined_delimiter_array = array_keys($delimiter_count_array, max($delimiter_count_array));

        while( $element = each( $determined_delimiter_array ) ){
        $determined_delimiter_count = $element['key'];
        $determined_delimiter = $element['value'];
        }

        $images = explode("{$determined_delimiter}", $get_images);
+3
source share
5 answers

, (, ,, ; |) , (substr_count). explode.

, , ;)

+8

, 99,99% :) , . script . .

, :

function readCSV($fileName)
{
    //detect these delimeters
    $delA = array(";", ",", "|", "\t");
    $linesA = array();
    $resultA = array();

    $maxLines = 20; //maximum lines to parse for detection, this can be higher for more precision
    $lines = count(file($fileName));
    if ($lines < $maxLines) {//if lines are less than the given maximum
        $maxLines = $lines;
    }

    //load lines
    foreach ($delA as $key => $del) {
        $rowNum = 0;
        if (($handle = fopen($fileName, "r")) !== false) {
            $linesA[$key] = array();
            while ((($data = fgetcsv($handle, 1000, $del)) !== false) && ($rowNum < $maxLines)) {
                $linesA[$key][] = count($data);
                $rowNum++;
            }

            fclose($handle);
        }
    }

    //count rows delimiter number discrepancy from each other
    foreach ($delA as $key => $del) {
        echo 'try for key=' . $key . ' delimeter=' . $del;
        $discr = 0;
        foreach ($linesA[$key] as $actNum) {
            if ($actNum == 1) {
                $resultA[$key] = 65535; //there is only one column with this delimeter in this line, so this is not our delimiter, set this discrepancy to high
                break;
            }

            foreach ($linesA[$key] as $actNum2) {
                $discr += abs($actNum - $actNum2);
            }

            //if its the real delimeter this result should the nearest to 0
            //because in the ideal (errorless) case all lines have same column number
            $resultA[$key] = $discr;
        }
    }

    var_dump($resultA);

    //select the discrepancy nearest to 0, this would be our delimiter
    $delRes = 65535;
    foreach ($resultA as $key => $res) {
        if ($res < $delRes) {
            $delRes = $res;
            $delKey = $key;
        }
    }

    $delimeter = $delA[$delKey];

    echo '$delimeter=' . $delimeter;

    //get rows
    $row = 0;
    $rowsA = array();
    if (($handle = fopen($fileName, "r")) !== false) {
        while (($data = fgetcsv($handle, 1000, $delimeter)) !== false) {
            $rowsA[$row] = Array();
            $num = count($data);
            for ($c = 0; $c < $num; $c++) {
                $rowsA[$row][] = trim($data[$c]);
            }
            $row++;
        }
        fclose($handle);
    }

    return $rowsA;
}
+3

, CSV , CSV -, ... ​​

protected function detectDelimiter() {
    $handle = @fopen($this->CSVFile, "r");
    if ($handle) {
        $line=fgets($handle, 4096);
        fclose($handle);            

        $test=explode(',', $line);
        if (count($test)>1) return ',';

        $test=explode(';', $line);
        if (count($test)>1) return ';';

        //.. and so on
    }
    //return default delimiter
    return $this->delimiter;
}
+2
source

I did something like this:

$line = fgetcsv($handle, 1000, "|");
if (isset($line[1]))
    {
    echo "delimiter is: |";
    $delimiter="|";
    }
    else
    {
    $line1 = fgetcsv($handle, 1000, ";");
    if (isset($line1[1]))
        {
        echo "delimiter is: ;";
        $delimiter=";";
        }
        else
        {
        echo "delimiter is: ,";
        $delimiter=",";
        }
    }

This just checks to see if there is a second column after reading the row.

-1
source

I have the same problem. My system will receive CSV files from the client, but can use ";", "," or "" as a separator, and I want to improve the system so that the client does not know what (they never do).

I found and found this library: https://github.com/parsecsv/parsecsv-for-php

Very nice and easy to use.

-1
source

Source: https://habr.com/ru/post/1772152/


All Articles