I have a function
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;
}
Next I call this function
$file = $article->saveImage($request, 'image_detail', '/storage/article/' .$article->id. '/');
The question now is, I have a $requestField
, which now has the value 'image_detail'
it should have this meaning everywhere, except for these lines
$pathFull. $requestField. '-'. $uploadname
$path. $requestField. '-'. $uploadname
I want the field $requestField
to be converted to such a value 'image-detail'
, that is, to replace the underscore '_'
with a dash '-'
, is it possible to do this at all in this function only for separate lines?
CodePudding user response:
The Str::replace method replaces a given string within the string:
use Illuminate\Support\Str;
$your_variable = 'image-detail';
$replaced = Str::replace('-', '_', $your_variable);
// image_detail