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.

Recommended Answers

All 6 Replies

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);
}

?>

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

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.

Member Avatar for diafol

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).

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.