0

Hello buddies and merry christmas
I have a string which is a file name with multi parts (my file.part001) I need to remove the (.part001) form the string so only the (my file) still.
I've tried

function removeFromEnd($string, $Remove) {
    $RemoveLen = strlen($Remove);
    $stringLen = strlen($string);

    $pos = $stringLen - $RemoveLen;

    $out = substr($string, 0, $pos);

    return $out;
}

but the problem I have numbers after (part) string
how to do somthing like this so any file with (part01)(part001)(part0001) witll be removed.

4
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by OsaMasw
1

Why don’t you use the last dot as a delimiter?
Example:

<?php
$string = "file.part001";
var_dump(beforeLast($string, "."));

function beforeLast($string,$search)
{
  $lastIndex = mb_strrpos($string, $search);
  return $lastIndex == 0 ? $string : mb_substr($string, 0 ,$lastIndex);
}

?>

Edited by jkon

0

Because if the filename was (my.file.part001) this will be incorrect becuase it has two dots.

0

Because if the filename was (my.file.part001) this will be incorrect becuase it has two dots.

Have you tried it? The function I gave you in this example is named beforeLast so it does what its name suggest (returns the string before the last occurrence of the search substring given) witch in this case is the dot.

0

With cereal on this one, pathinfo() will return all the useful properties of the file or you can limit the return values with a constant parameter (PATHINFO_FILENAME being the one you need).

Edited by diafol

0

the pathinfo() function return the full filename, I've tried it but its return the (part001) with it, I used Before last and delete the last three letters, since the .rar splitting always has three numbers after (part) string, so I nearly solved my problem.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.