Question

How would I remove the white space from something like, this is a example and change that to thisisaexample i am using this with mkdir. I tried the trim() and the str_replace(' ', '', mkdir()) but neither seemed to work.

code to make directories:

if (isset($_POST['mkdir']) && !empty($folder_name)) {
    mkdir($directory . $folder_name);
    mkdir($directory . $folder_name . '/uploads/'));
    mkdir($directory . $folder_name . '/uploads/' . $_SESSION['user']);
Was it helpful?

Solution

I think you're doing something else wrong. Try:

mkdir( str_replace(' ', '', "this is an example") )

OTHER TIPS

Use preg_replace():

$folder_name = 'this is a example';
$folder_name = preg_replace('/\s+/', '', $folder_name);
if (isset($_POST['mkdir']) && !empty($folder_name)) {
    mkdir($directory . $folder_name);
    mkdir($directory . $folder_name . '/uploads/'));
    mkdir($directory . $folder_name . '/uploads/' . $_SESSION['user']);
}

CAUTION: If you are using user input for this, this will not be safe. You must do more than check for white space in that context.

Do like this..

<?php
$dir = 'this is a example';
$dir = trim(str_replace(' ','',$dir));
mkdir($dir, 0777);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top