William Sandoval William Sandoval - 1 year ago 69
PHP Question

Image POST php slug

I'm trying to make a slug name to image while upload, i was testing with

but it doesn't work.

$_FILES['imgProfile']['name'] = str_replace("í", "i", $_FILES['imgProfile']['name']);

it returns something like:
and doesn't upload the image.

I tried with this function and works, but removes the file extension.

function slugify($text)
// replace non letter or digits by -
$text = preg_replace('~[^\pL\d]+~u', '-', $text);

// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);

// trim
$text = trim($text, '-');

// remove duplicate -
$text = preg_replace('~-+~', '-', $text);

// lowercase
$text = strtolower($text);

if (empty($text)) {
return 'n-a';

return $text;

I only need to remove
blank spaces
and characters like

If the file name is:
"prueba para Guardar ímagen ñueva.png"

it must be:


Answer Source

For your desired behavior you need to add to first row \.

// replace non letter or digits by -
$text = preg_replace('~[^\pL\d\.]+~u', '-', $text);

and for third row too \.

// remove unwanted characters
$text = preg_replace('~[^-\w\.]+~', '', $text);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download