Reputation: 7798
I have a file name as Dona&ld #Duck .jpg
. I want to replace all the special characters before .jpg or any file extension with _
(underscore).
Output should be Don_a_ld__Duck__.jpg
How to achieve this using preg_replace? I have done as following.
$fileName ='Don.a&ld #Duck .jpg';
$fileName = trim($fileName);
$fileExtension = substr($fileName,-4);
$fileName = substr($fileName,0,-4); // file name without extension
echo $filename = preg_replace('/[^a-z0-9]/i', '_', $fileName).$fileExtension;
I do not want append file extension separately.
Upvotes: 1
Views: 1185
Reputation: 627536
Use the following solution with a negative lookahead that protects the last dot:
$fileName ='Don.a&ld #Duck .jpg';
echo $filename = preg_replace('/[^a-z0-9](?![^.]*$)/i', '_', $fileName);
^^^^^^^^^
See the PHP demo
The (?![^.]*$)
just fails the match if a special char is followed by 0+ chars other than a dot up to the end of the string.
Note that /[^a-z0-9]/i
matches _
and replaces with _
, which is a redundant operation. You may avoid it with a \W
subpattern:
preg_replace('/\W(?![^.]*$)/', '_', $fileName)
Upvotes: 2
Reputation: 786289
You can use lookahead regex:
$fileName = preg_replace('/\W(?=.*\.[^.]+$)/', '_', $fileName);
//=> Dona_ld__Duck__.jpg
\W(?=.*\.[^.]+$)
will match a non word character that is followed by DOT and any extension.
Upvotes: 2