我想知道可以执行以下操作的功能
$ str ="uploads/file/file1.jpg";
我想削减字符串uploads/file /的初始值,只想将file1.jpg作为值返回.
我尝试使用str_replace(),
$str = "uploads/file/file1.jpg";
$str2 = str_replace("uploads/file"," ", $str);
echo $str;
Run Code Online (Sandbox Code Playgroud)
这不行,我哪里错了?
编辑:愚蠢的我,我没有注意到我试图回应$ str,对不起.它现在对我有用.
BTW我想知道哪种方法比上面的方法更好或者basename();
使用basename():它专门用于从路径中获取文件名部分.
$str2 = basename("uploads/file/file1.jpg"); // Will return file1.jpg
Run Code Online (Sandbox Code Playgroud)
同样有趣的是(虽然在本次考试中没有必要)pathinfo()将字符串拆分为四个组件:目录,基本名称,文件名和扩展名.
相当于将URL拆分为其组件parse_url().