IDA
IDA

Reputation: 37

Remove spaces from values in the array explode() returns

I got this code that will :

  1. Ask user to upload .docx which contains format like this (erina, natasha culler, danial joshstone)
  2. After they upload the list name will be inserted to the database. Every name got teir own row in database.

The code below is running well. But after the name is inserted, the database becomes like this:

If you see the first name erina, you can see that it got a big space. But rest of the names were inserted perfectly. It's just for the first one. I dont know why. Because of that space I cant search query the erina name. I tried many things, but still got that result.

     <?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);
}
?>

How to fix this problem?

Upvotes: 1

Views: 2755

Answers (3)

hel
hel

Reputation: 503

Using trim() will help to remove whitespace in each word.

$query .= "('" . $id . "','" . trim($friend) . "'),";

You can refer to the documentation of trim() function here, for further functionality of the same.

Upvotes: 3

user1978142
user1978142

Reputation: 7948

Alternatively, If you want to remove to much spacing on each exploded value, you could use array_map then trim. Consider this example:

// 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>";

Upvotes: 3

FuzzyTree
FuzzyTree

Reputation: 32392

Use trim() to remove whitespace before inserting

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

Upvotes: 2

Related Questions