php - 覆盖变量中的名称
问题描述
我有一个功能
public function saveImage(Request $request, $requestField, $path) {
if ($request->hasFile($requestField)) {
$image_path = public_path($this->{ $requestField });
if (File::exists($image_path)) {
File::delete($image_path);
}
$file = $request->file($requestField);
$uploadname = $this->getUploadName($file);
$pathFull = public_path($path);
if (!File::exists($pathFull, 0775, true)) {
File::makeDirectory($pathFull, 0775, true);
}
Image::make($file)->save($pathFull. $requestField. '-'. $uploadname);
$this->{ $requestField } = $path. $requestField. '-'. $uploadname;
return $file;
}
return false;
}
接下来我调用这个函数
$file = $article->saveImage($request, 'image_detail', '/storage/article/' .$article->id. '/');
现在的问题是,我有一个$requestField
,它现在具有价值'image_detail'
它应该在任何地方都有这个含义,除了这些行
$pathFull. $requestField. '-'. $uploadname
$path. $requestField. '-'. $uploadname
我希望将字段$requestField
转换为这样的值'image-detail'
,即'_'
用破折号替换下划线'-'
,是否可以在此函数中仅针对单独的行执行此操作?
解决方案
Str::replace方法替换字符串中的给定字符串:
use Illuminate\Support\Str;
$your_variable = 'image-detail';
$replaced = Str::replace('-', '_', $your_variable);
// image_detail
推荐阅读
- amazon-web-services - 我应该如何跨 VPC 分配共享服务的暂存版本?
- c# - 在 HTML Div 中打印文件内容
- c - 如何使用 Memmove 和 Memset 删除列表中的项目?
- reactjs - 在更改路由或检测到新构建存在时刷新部署在 firebase 托管上的构建
- python - 如何为熊猫列中的每个逗号分隔值创建一个新行
- apache-spark - LOG4J 日志记录在 Spark 纱线集群模式下不起作用
- java - 多大的 HTTP 响应被认为是巨大的?
- javascript - 如何在 html 视图中呈现图像数组?
- android - 如何从geoJson源Mapbox获取所有功能
- python - Python 新手 - 需要安装帮助