user3648426
user3648426

Reputation: 251

making file name lower case while preserving extension

Suppose I have the following piece of code:

  $myString = 'FilE.EXE';

  strlower($myString);

I want to make the name minus its extension to lower case, but the code above will make the entire string into lower case. Is there a way I can just change the name without the extension? If so, what is the most dynamic way to accomplish this?

Desired output: 'file.EXE';

Upvotes: 0

Views: 665

Answers (5)

Avinash Babu
Avinash Babu

Reputation: 6252

<?php
$myString = 'FilE.EXE';
$txt = strtolower( substr( $myString, 0, strrpos($myString, ".") ) );
$hell  = substr( $myString, strrpos($myString, "."),  strlen($myString));
$babe = $txt.$hell;
echo $babe;

Upvotes: 1

fpietka
fpietka

Reputation: 1043

You might want to use the pathinfo() function for that:

$myString = 'FilE.iNc.EXE';

$path_parts = pathinfo($myString);

$myNewString = implode('.', array(
    strtolower($path_parts['filename']),
    $path_parts['extension']
));

So it can ouput this:

file.inc.EXE

Upvotes: 1

kapa89
kapa89

Reputation: 615

You need to do something like this:

$string = "FilE.EXE";
list($name, $extension) = explode('.', $string);
$string = implode('.', array(strtolower($name), $extension));

Hope it helps.

Upvotes: 2

Amit Yadav
Amit Yadav

Reputation: 46

Using pathinfo

$myString = 'FilE.EXE'; $new_string = strtolower(pathinfo($myString, PATHINFO_FILENAME)) . '.' . pathinfo($myString, PATHINFO_EXTENSION); echo $new_string;

Upvotes: 3

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

do:

$myString = 'FilE.EXE';
$txt = strtolower( substr( $myString, 0, strrpos($myString, ".") ) )
          .substr( $myString, strrpos($myString, "."),  strlen($myString));
echo $txt; //gives file.EXE 

Upvotes: 1

Related Questions