开发者

sub string replacment in PHP

开发者 https://www.devze.com 2023-01-27 19:07 出处:网络
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\";

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

0

精彩评论

暂无评论...
验证码 换一张
取 消