How to use function like 'rtrim' in drupal? I need to remove extension from string, i'm wondering what function could do that ?

Some like the rtrim($string,'string to remove')

$string could be the filename

I've added a file into database, now to retrieve it, i want to remove the extension '.epub'

this is the postman response :

{
 "code": "0",
 "response": [
{
  "epub": "http://localhost/drupal/sites/default/files/moby-dick.epub",
  "f_id": "5",
  "uid": "64"
   }
 ]
 }


I want to retrieve :

"epub": "http://localhost/drupal/sites/default/files/moby-dick",&lt;== no .epub


This is my first try :

$file = file_load($fid);
            //$realURI = drupal_realpath($file-&gt;uri); ca marche


            $realURI = file_create_url($file-&gt;uri);
            $new = drupal_basename($realURI, '.epub');
            $n = file_create_url($new);
            $res-&gt;epub = $realURI;&lt;== show the new (without .epub)


but now it shows :

"epub": "http://localhost/drupal/moby-dick"


I need to keep /sites/default/files/ because the file is there 
What can i add for keep it work with the right path please

Thank you
Use the Drupal file API to do this; file_move() will take care of renaming.

  $file = file_load($fid);
  $new_file_uri = preg_replace('/\\.([a-zA-Z]+)$/', '', $file-&gt;uri); //Remove .whatever from the file name
  file_move($file, $new_file_uri);