I have a string like: $string = "/physics/mechanics/vectors/P-M-C (1).doc";
I want to get like this:
"/physics/mechanics/vectors/1-P-M-C (1).doc";
Please note that "1-" is added just before P in the original string.
Is it possible in PHP? How the function in PHP should be us开发者_运维知识库ed?
@fawad:Here's a sample to get you started --
$oldstring = "/physics/mechanics/vectors/P-M-C (1).doc";
$parts = explode("/", $oldstring);
$file = $parts[count($parts) - 1];
$newstring = str_replace($file, "1-" . $file, $oldstring);
Take a look at explode()
and join()
for this. :)
精彩评论