1

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?

4
  • Add this line after $friendarray = explode(",", $friendslist);: $friendarray = array_map('trim', $friendarray);. Commented Jun 3, 2014 at 3:47
  • @AmalMurali yes dear...everyone said use the trim() function.. I really forgot about trim()..so embarrassed..haha.. thank you very much dear for your reply and edited post dear.. :) Commented Jun 3, 2014 at 4:00
  • No problem, @rita. I'm not sure if you need to add the "dear" thing in all your comments. It's... not very useful. Just saying. Commented Jun 3, 2014 at 4:12
  • @AmalMurali haha..its ok..I understand..just over excited with my happy emotion.. women + over emotion = bad.. :) thanks again..case closed.. Commented Jun 3, 2014 at 4:21

3 Answers 3

3

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>";
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2

Use trim() to remove whitespace before inserting

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.