title: remove spaces from a file name string PHP
body: <p>I'm creating an image upload form and I need to name the images the user's name and dog's name when it is added into my database, and added into my upload folder.</p>
  
  <p>I currently have this working like this:</p>
  
  <ol>
  <li>I use the POST method at the top of my script to pull in the user
  name and dog name from the form.</li>
  <li>I then use that info to rename the file/image being uploaded</li>
  <li>Then I insert into my database</li>
  </ol>
  
  <hr>
  
  <pre><code>$user_name = $_POST['user_name'];
  $dog_name  = $_POST['dog_name'];
  
  $file_name = $imgDir . $user_name .'-'. $dog_name .'-'. $random_number . $ext;
  </code></pre>
  
  <p>When inserting into the table I use the following:</p>
  
  <pre><code>'file_name' =&gt; str_replace("../path/to/images/", "", $file_name),
  </code></pre>
  
  <p>The current output of the file name is like this:
  <strong>John-Fido-123456.jpg</strong></p>
  
  <p>How would I remove the spaces in file name if the user entered their last name like this?</p>
  
  <p><strong>John Doe-Fido Doe-123456.jpg</strong></p>
  
  <hr>
  
  <p>Update: Thanks to @HiDeo i used the dup link that he posted and was able to figure it out like this. </p>
  
  <p>Using the Post method to grab the names I needed. I striped the white spaces like so and it worked on the file name in the database and the actual image in the folder.</p>
  
  <pre><code>$user_name  =   $_POST['user_name'];
  $dog_name   =   $_POST['dog_name'];
  
  
  $user_name = preg_replace('/\s+/', '', $user_name);
  $dog_name  = preg_replace('/\s+/', '', $dog_name );
  </code></pre>