Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php在excel中分隔带有逗号的全名_Php_Sql_Phpexcel - Fatal编程技术网

如何使用php在excel中分隔带有逗号的全名

如何使用php在excel中分隔带有逗号的全名,php,sql,phpexcel,Php,Sql,Phpexcel,我有一个项目,它从excel中获取名称,并将值存储在名字和姓氏中。问题是在excel文件中存储了名称(例如John,Constantine),如何获取John和Constantine并将其存储在两个不同的变量中 if(isset($_POST['excel_btn'])) { require('import/PHPExcel/PHPExcel.php'); require('import/PHPExcel/PHPExcel/IOFactory.php'); $file

我有一个项目,它从excel中获取名称,并将值存储在名字和姓氏中。问题是在excel文件中存储了名称(例如John,Constantine),如何获取John和Constantine并将其存储在两个不同的变量中


if(isset($_POST['excel_btn']))
{
    require('import/PHPExcel/PHPExcel.php');
    require('import/PHPExcel/PHPExcel/IOFactory.php');

    $file=$_FILES['myFile']['tmp_name'];
    

    $obj=PHPExcel_IOFactory::load($file);
    foreach($obj->getWorksheetIterator() as $sheet)
    {
        $getHighestRow=$sheet->getHighestRow();
        for($i=1; $i<=$getHighestRow; $i++){
            $name=$sheet->getCellByColumnAndRow(0,$i)->getValue();
           
             if($name !=''){
                 $query = "INSERT INTO users(name) VALUES('$name')";
            
            $query_run=mysqli_query($conn, $query);

            }
    }
}


如果(isset($\u POST['excel\u btn']))
{
要求('import/PHPExcel/PHPExcel.php');
要求('import/PHPExcel/PHPExcel/IOFactory.php');
$file=$\u文件['myFile']['tmp\u名称'];
$obj=PHPExcel_IOFactory::load($file);
foreach($obj->getWorksheetIterator()作为$sheet)
{
$getHighestRow=$sheet->getHighestRow();
对于($i=1;$igetCellByColumnRow(0,$i)->getValue();
如果($name!=''){
$query=“插入用户(名称)值(“$name”)”;
$query\u run=mysqli\u query($conn,$query);
}
}
}
这是我到目前为止所写的,但是使用它,全名存储在变量
$name

(更新)

使用:

或者,作为@Markus Zeller:

list($first, $last) = explode(',', 'John, Constantine')
echo trim($first);
echo trim($last);

你也可以使用一个正则表达式,它将被逗号或任何空格字符分割。可选标志PREG_split_NO_EMPTY将确保不会返回空匹配项。这个技巧还可以确保修剪

list($first, $last) = preg_split('/[,\s*]/', 'John, Doe', -1, PREG_SPLIT_NO_EMPTY);
这也适用于像“
John,Doe
”这样的字符串,两个字符串上的结果都是

$first = 'John';
$last  = 'Doe';
list($first,$last)=explode(',','John,Constantine')
不要尝试只使用
explode()
函数。用逗号分割(不带空格)也会更安全,比如
,'
然后使用
trim()
-
echo trim($pieces[1]);
$first = 'John';
$last  = 'Doe';