Jump to content

Clean up image name after uploading


Presto-X

Recommended Posts

Hello everyone,

 

I want to clean up image names after they have been uploaded, I only want to use [a-z] and [0-9] and turn spaces in to underscores?

 

I was trying to use this but it took out the period before the image extension :(

 

$image = strtolower(preg_replace("/^[a-z0-9][a-z0-9\-]+[a-z0-9]$/i", "", $image));

 

These are some of the other ways I have tride, I think I am close just not quite there yet:

$targetChars = array(" ", "%20", "[", "]", "(", ")", "'", "-");
$image = str_replace($targetChars, "_", $image);
$image = preg_replace('/^\W+|\W+$/', '', $image);
$image = preg_replace('/\s+/', '_', $image);
$image = strtolower(preg_replace('/\W-/', '', $image));
$image = preg_replace('/[^a-z0-9]/', '', $image);
$image = strtolower(preg_replace("/^[a-z0-9][a-z0-9\-]+[a-z0-9]$/i", "", $image));
$image = preg_replace('/[^\w\d_ -]/si', '', $image);

Link to comment
https://forums.phpfreaks.com/topic/181959-clean-up-image-name-after-uploading/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.