code:
$matches = glob("$searchword*.txt", GLOB_BRACE) ;
that works, but i also have $secondword, so i read How to define multiple patterns in php glob()
so i tried
$matches = glob("{$searchword},{$secondword}*.txt", GLOB_BRACE) ;
$matches = glob("{$searchword,$secondword}*.txt", GLOB_BRACE) ;
$matches = glob("{$searchword*.txt},{$secondword*.txt}", GLOB_BRACE) ;
$matches = glob("$searchword*.txt", GLOB_BRACE) && ("$secondword*.txt", GLOB_BRACE);
$matches = (glob("$searchword*.txt", GLOB_BRACE) && ("$secondword*.txt", GLOB_BRACE));
results in invalid syntax
what im trying to do: list files via glob that are smilar to a $filename
is it possible to glob files that are similar to $filename?
references:
https://www.cowburn.info/2010/04/30/glob-patterns/
I've multiple txt files on my directory. Here I used glob with GLOB_BRACE and it perfectly showing the expected result to me.
$a = 'import_start_';
$b = '22_02_18_country_';
$m= glob("{{$a},{$b}}*.txt", GLOB_BRACE);
// OR //
// $m= glob({".$a.",".$b."}*.txt", GLOB_BRACE);
// OR //
print '<pre>';
print_r($m);
print '</pre>';
Output:
Array
(
[0] => import_start_date.txt
[1] => 22_02_18_country_list.txt
)
Related
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.
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
The foreach loop is statement also storing temp path of image to $images
Change variable name in foreach
$images=$request->file('images') tto $image=>$request->file('images')
I will go with this way, hope this helps you.
$images= [];
if($request->hasFile('images')){
foreach($request->file('images') as $img) {
$name = "some_random_sting";
$extension = $img->getClientOriginalExtension();
$imgName = $name .'.'.$extension;
$img->move(public_path().'/dpic', $imgName);
$images[] = $imgName;
}
}
$test = implode(", ", $images);
$product->images = $test;
i have a variable that shows me the path to a directory like below:
$dir = uploads/sha256/folder1/subfolder1/subsubfolder1
How can i "cut off" the first 2 directories from $dir so that it becomes:
$dir = folder1/subfolder1/subsubfolder1
sample code:
$dir = "uploads/sha256/folder1/subfolder1/subsubfolder1";
$pieces = explode("/", $dir);
echo $pieces[2]; // piece2
This gives me only folder1
And i need the complete path after the sha256
so what i actually try to achieve is something like this:
echo $pieces[>2];
You can capture () everything after the first two directories and replace with that:
$dir = preg_replace('#[^/]+/[^/]+/(.*)#', '$1', $dir);
Or you can explode it, slice all elements after the first two and implode it again:
$dir = implode('/', array_slice(explode('/', $dir), 2));
I want Get Matched Files in php I have try Many More Time
for example ...
my directory files
1.254450_abcd.mp3
2.101215_apple.mp4
3.102545_efgf.php
i find only number like this 254450
$mypath = "/files/" ;
$find = "254450" ;
//i want get matched full name
echo "$filename" ;// get 254450_abcd.mp3
else
"file not found " ;
You can use scandir and preg_grep.
$mypath = "/files/" ;
$find = "254450" ;
$files = scandir($mypath);
$matches = preg_grep("/" . $find . "/", $files);
$matches is now an array with files matching $find
Here is a semi working example. I replaced scandir with your files in an array, just like scandir returns them.
https://3v4l.org/QullZ
I need to get the contents of a text file called file.txt. The contents of that file are:
word1,word 2,word 3 1,another word 1,
I have a config.php which includes:
$file = "file.txt";
$value = explode(",", $file);
And script.php file which will execute other commands based on $value which includes:
if (count(explode($value, $chat))>1) {
After that, it will execute a command if the $value was detected in $chat. So, I need the $value to be a separate word in file.txt.
How can I do this?
If you're looking for more flexibility, you might want to try using preg_split rather than explode, which splits on a regular expression. For example, to split on newlines and commas, you could use this:
$text = file_get_contents('text.txt');
$values = preg_split('/[\n,]+/', $text);
Testing it out:
$s = "word1,word 2\n word 3";
print_r(preg_split('/[\n,]+/', $s));
Output:
Array
(
[0] => word1
[1] => word 2
[2] => word 3
)
Putting that into your script:
$file = "file.txt";
$text = file_get_contents($file);
$values = preg_split('/[\n,]+/', $text);
Then $values is an array, which you can loop over in the other script:
foreach ($values as $value) {
// do whatever you want with each value
echo $value;
}
Reading file contents:
file_get_contents
Explode string:
explode
Get file as array (each line => one item):
file
BTW: a short google would already answer your question...
In config.php add this code, be sure that the file is in the same folder
$value = file_get_contents('file.txt');
Then in script.php add this code:
$pieces = explode(",", $value);
Read more about file_get_contents and explode. (Click on the names)
I'm trying to extract filenames from a list of files with pathes like :
/a/b/c/d/file1.jpg
/e/f/g/h/file2.png
/i/j/k/l/file3.txt
I want to get a string that is a valid filename (for linux) that is between a "/" is a jpeg file (ends with ".jpg").
In this example, "file1" would be the only valid match.
At the moment I have this RegEx :
/(?<=\/)(.*?)(?=\.(js))/gim
I don't really know if it's better to do this with RegEx or if it's better / possible with basename().
The goal I want to achieve is to get all the strings that match to be placed in an array.
Don't know if I'm doing this right though.
Regex isn't required here. I've assumed you can get your paths into an array.
<?php
$text = file_get_contents("list.txt");
$foo = explode(PHP_EOL, $text);
$bar = array();
foreach($foo as $key => $value){
if(pathinfo($value, PATHINFO_EXTENSION) == "jpg"){
$bar[] = basename($foo[$key],".".pathinfo($value, PATHINFO_EXTENSION));
}
}
print_r($bar);
?>
Outputs:
Array ( [0] => file1 )
Live example: http://codepad.viper-7.com/ewkUHs