在PHP中不使用潜在的分隔符检测分隔符。

时间:2022-09-23 00:09:32

I need to use a function in PHP that detect a delimiter of a file using 2 parameters:

我需要在PHP中使用一个函数,它使用两个参数检测文件的分隔符:

the file handle and an array of the expected header values.

文件句柄和预期头值的数组。

Can someone please help me in writing the function or using a similar existing function that use the header of the file and match it with the expected header values to detect the file delimiter without using an array of potential delimiters.

是否有人可以帮助我编写该函数或使用一个类似的现有函数,该函数使用该文件的头,并将其与预期的头值相匹配,以在不使用潜在分隔符数组的情况下检测文件分隔符。

Thank you

谢谢你!

1 个解决方案

#1


0  

<?php

    $col1 = 'name';
    $enc = '"';
    $line = '"name";"company";"email"';

    $delimiter = substr($line, (substr($line, 0, 1) == $enc ? (strlen($col1)+2) : strlen($col1)), 1);
    echo $delimiter;

?>

Output

输出

;

This first checks if the 1st character in $line is ". If there is no ", it substrings 1 character with a starting position of the length of $col1. If there are ", it substrings 1 character with a starting position of the length of $col1 +2 (for each of the ")

这首先检查$line中的第一个字符是否为“。”如果没有“,它将substrings 1字符的起始位置为$col1。如果有“,它将以$col1 +2(每一个)的起始位置为起始位置的子字符串1。

#1


0  

<?php

    $col1 = 'name';
    $enc = '"';
    $line = '"name";"company";"email"';

    $delimiter = substr($line, (substr($line, 0, 1) == $enc ? (strlen($col1)+2) : strlen($col1)), 1);
    echo $delimiter;

?>

Output

输出

;

This first checks if the 1st character in $line is ". If there is no ", it substrings 1 character with a starting position of the length of $col1. If there are ", it substrings 1 character with a starting position of the length of $col1 +2 (for each of the ")

这首先检查$line中的第一个字符是否为“。”如果没有“,它将substrings 1字符的起始位置为$col1。如果有“,它将以$col1 +2(每一个)的起始位置为起始位置的子字符串1。