I am inserting multiple images on server and storing there name in SQL database by (,) seperated using this.
if($request->hasFile('images')){
$images= [];
foreach($images=$request->file('images') as $img) {
$name=$img->getClientOriginalName();
$img->move(public_path().'/dpic', $name);
$images[]=$name;
}
}
$test =implode(", ", $images);
$product->images =$test;
Image name are inserting into database along with some data it shows output like.
/tmp/php59iuBb, /tmp/phpdRewVH, PicturesI.jpg, Screenshot.png
I want to remove this /tmp/php59iuBb, /tmp/phpdRewVH
from output How can I do that.
please guide me to do so.
The splice method can also be used to remove a range of elements from an array. If you know the value you want to remove from an array you can use the splice method. First you must identify the index of the target item. You then use the index as the start element and remove just one element. This is a simple example where the elements are integers.
If you know the value you want to remove from an array you can use the splice method. First you must identify the index of the target item. You then use the index as the start element and remove just one element. This is a simple example where the elements are integers.
Instead of a delete method, the JavaScript array has a variety of ways you can clean array values. You can remove elements from the end of an array using pop, from the beginning using shift, or from the middle using splice.
The pop method modifies the array on which it is invoked, This means unlike using delete the last element is removed completely and the array length reduced. How do you remove the first element of a JavaScript array?
I would do this
$images =[
'/tmp/php59iuBb', '/tmp/phpdRewVH', 'PicturesI.jpg', 'Screenshot.png'
];
$images = preg_grep('~^(?!/tmp/)~', $images);
print_r($images);
Output
Array
(
[2] => PicturesI.jpg
[3] => Screenshot.png
)
Sandbox
Simple right!
Preg grep runs a regular expression against an array and returns the matches.
In this case
~^(?!/tmp/)~
negative lookbehind - insures that the match does not start with /tmp/
Which leaves us what we want.
Another option is
$images = array_filter($images,function($image){
return substr($image, 0, 5) != '/tmp/';
});
If you are not feeling the Regex love.
Sandbox
PS I love preg_grep its often overlooked for easier to understand but much more lengthy code. Preg Filter is another one of those, which you can use to prefix or suffix an entire array. For example I've used it to prepend paths to an array of filenames etc. For example it's this easy:
$images =[
'/tmp/php59iuBb', '/tmp/phpdRewVH', 'PicturesI.jpg', 'Screenshot.png'
];
print_r(preg_filter('~^(?!/tmp/)~', '/home/images/', $images));
//or you can add a whole image tag, if you want, with a capture group (.+) and backrefrence \1
print_r(preg_filter('~^(?!/tmp/)(.+)~', '<img src="/home/images/\1" />', $images));
Output
Array
(
[2] => /home/images/PicturesI.jpg
[3] => /home/images/Screenshot.png
)
Array
(
[2] => <img src="/home/images/PicturesI.jpg" />
[3] => <img src="/home/images/Screenshot.png" />
)
Sandbox
I thought you may find that "trick" useful as you can remove the bad ones and add a path to the good at the same time. They are worth checking out.
http://php.net/manual/en/function.preg-grep.php
http://php.net/manual/en/function.preg-filter.php
I feel like I should mention the same holds true for matching a file extension, which may also be useful, but I will leave that for another day.
Cheers!
Bit late to the party, but I would personally prefer using pathinfo
over regular expressions here, since it's dedicated to file paths:
$images = ['/tmp/php59iuBb', '/tmp/phpdRewVH', 'PicturesI.jpg', 'Screenshot.png'];
$images = array_filter($images, function ($image) {
return pathinfo($image, PATHINFO_DIRNAME) !== '/tmp';
});
print_r($images);
Demo: https://3v4l.org/6F6K8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With