Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.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 从数组explode()返回值中删除空格_Php_Explode - Fatal编程技术网

Php 从数组explode()返回值中删除空格

Php 从数组explode()返回值中删除空格,php,explode,Php,Explode,我得到了以下代码: 要求用户上传包含以下格式的.docx(erina,natasha culler,danial joshstone) 上传后,列表名将插入数据库。每个名字在数据库中都有自己的行 下面的代码运行良好。但在插入名称后,数据库将如下所示: 如果您看到名字erina,您可以看到它有一个很大的空格。但其余的名字被完美地插入。这只是第一次。我不知道为什么。因为这个空间,我无法搜索和查询erina的名字。我尝试了很多东西,但仍然得到了那个结果 <?php inclu

我得到了以下代码:

  • 要求用户上传包含以下格式的
    .docx
    erina
    natasha culler
    danial joshstone
  • 上传后,列表名将插入数据库。每个名字在数据库中都有自己的行 下面的代码运行良好。但在插入名称后,数据库将如下所示:

    如果您看到名字
    erina
    ,您可以看到它有一个很大的空格。但其余的名字被完美地插入。这只是第一次。我不知道为什么。因为这个空间,我无法搜索和查询erina的名字。我尝试了很多东西,但仍然得到了那个结果

         <?php
        include 'configure.php';
    
        if(isset($_FILES['uploaded_file'])) 
        {
    
        $document_path = $_FILES ['uploaded_file']['tmp_name'];
        $document_name=$_FILES ['uploaded_file']['name'];
    
        function extracttext($filename,$filepath) 
        {
            $ext = explode('.', $filename);
            $ext=end ($ext);
            if($ext == 'docx')
            $dataFile = "word/document.xml";
            else
            $dataFile = "content.xml";    
    
            $zip = new ZipArchive;
    
            if (true === $zip->open($filepath)) 
           {
    
            if (($index = $zip->locateName($dataFile)) !== false) 
            {
                $text = $zip->getFromIndex($index);
    
                $xml = new DOMDocument;
                $xml->loadXML($text, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
    
                return strip_tags($xml->saveXML());
            }
    
            $zip->close();
          }
        return "File not found";
      }
    
        $friendslist = extracttext($document_name,$document_path);
    
        $id = "24";
    
        $friendarray = explode(",", $friendslist);
        $frienduserarray = array();
    
        for ($n = 0; $n < count($friendarray); $n++) 
        {
          $friendidpush = "('".$id."','".$friendarray[$n]."'),";
          array_push($frienduserarray, $friendidpush);
        }
    
    
        $query = "INSERT INTO keywords (criteria, value) VALUES ";
        $friendarray = explode(",", $friendslist);
    
        foreach ($friendarray as $friend) 
        {
               $query .= "('" . $id . "','" . $friend . "'),";
        }
    
          $query = substr($query, 0, -1); // remove trailing comma
    
          mysql_query($query);
    }
    ?>
    
    open($filepath))
    {
    if(($index=$zip->locateName($dataFile))!==false)
    {
    $text=$zip->getFromIndex($index);
    $xml=新文档;
    $xml->loadXML($text,LIBXML|NOENT | LIBXML|XINCLUDE | LIBXML|NOERROR | LIBXML|NOWARNING);
    返回strip_标记($xml->saveXML());
    }
    $zip->close();
    }
    返回“未找到文件”;
    }
    $friendslist=extracttext($document\u name,$document\u path);
    $id=“24”;
    $friendarray=explode(“,”,$friendslist);
    $frienduserarray=array();
    对于($n=0;$n
    
    如何解决此问题?

    在插入前使用
    trim()
    删除空白

    foreach ($friendarray as $friend) 
    {
           $query .= "('" . $id . "','" . trim($friend) . "'),";
    }
    
    插入前,使用
    trim()
    删除空白

    foreach ($friendarray as $friend) 
    {
           $query .= "('" . $id . "','" . trim($friend) . "'),";
    }
    

    或者,如果您想在每个分解值上删除较大的间距,可以使用
    array\u map
    ,然后使用
    trim
    。考虑这个例子:

    // this is just a sample!
    $friendarray = 'friend1,              friend2, friend3, friend4';
    $friendarray = explode(',', $friendarray);
    $friendarray = array_map('trim', $friendarray);
    echo "<pre>";
    var_dump($friendarray);
    echo "</pre>";
    
    //这只是一个示例!
    $friendarray='friend1,friend2,friend3,friend4';
    $friendarray=分解(“,”,$friendarray);
    $friendarray=array\u map('trim',$friendarray);
    回声“;
    变量转储($friendarray);
    回声“;
    
    或者,如果您想在每个分解值上删除足够多的间距,可以使用
    数组映射
    ,然后使用
    修剪
    。考虑这个例子:

    // this is just a sample!
    $friendarray = 'friend1,              friend2, friend3, friend4';
    $friendarray = explode(',', $friendarray);
    $friendarray = array_map('trim', $friendarray);
    echo "<pre>";
    var_dump($friendarray);
    echo "</pre>";
    
    //这只是一个示例!
    $friendarray='friend1,friend2,friend3,friend4';
    $friendarray=分解(“,”,$friendarray);
    $friendarray=array\u map('trim',$friendarray);
    回声“;
    变量转储($friendarray);
    回声“;
    
    使用trim()将有助于删除每个单词中的空白

    $query.=”(“$id.”、“.trim($friend.”)和“

    有关trim()函数的更多功能,请参阅trim()函数的文档。

    使用trim()将有助于删除每个单词中的空白

    $query.=”(“$id.”、“.trim($friend.”)和“


    您可以参考trim()函数的文档,以了解该函数的更多功能。

    $friendarray=explode(“,”,$friendslist)之后添加此行
    $friendarray=array\u map('trim',$friendarray)
    @AmalMurali是的,亲爱的……每个人都说使用trim()函数。。我真的忘了修剪……好尴尬……哈哈。。非常感谢您的回复和编辑帖子亲爱的:)没问题,丽塔。我不确定你是否需要在所有评论中添加“亲爱的”一词。是。。。不是很有用。只是说说而已。@AmalMurali哈哈..没关系..我明白..只是我的快乐情绪太激动了。。女人+过度情绪=不好:)再次感谢..案例结束..在
    $friendarray=explode(“,”,$friendslist)之后添加此行
    $friendarray=array\u map('trim',$friendarray)
    @AmalMurali是的,亲爱的……每个人都说使用trim()函数。。我真的忘了修剪……好尴尬……哈哈。。非常感谢您的回复和编辑帖子亲爱的:)没问题,丽塔。我不确定你是否需要在所有评论中添加“亲爱的”一词。是。。。不是很有用。只是说说而已。@AmalMurali哈哈..没关系..我明白..只是我的快乐情绪太激动了。。女人+过度情绪=不好:)再次感谢..案件结案。。