Directory searching in PHP with multiple criteria - php

I've been trying to find a way to search a directory with multiple criteria in PHP with little luck.
I have a folder that contains multiple versions of the same file but in different languages and for different devices:
e.g.
1. file_en_v980.jar
2. file_fr_v980.jar
3. file_en_v990.jar
4. file_fr_v990.jar
I want to be able to search the directory for all English (en) .jar files for the v980.
I've tried this:
foreach(glob('./'.$installer_location.'/'.$brand_name.'/*en*.jar') as $filename){
echo $filename."</br>";
}
It works, but returns the french (fr) file too.
I've tried this:
foreach(glob('./'.$installer_location.'/'.$brand_name.'/*en,v980*.jar') as $filename){
echo $filename."</br>";
}
But it returns nothing so I'm assuming I've got the syntax wrong.
Any help would be greatly appreciated.
Addition:
Based on the suggestion from the comment below I tried this:
foreach(glob('./'.$installer_location.'/'.$brand_name.'/*{en,v980}*.jar',GLOB_BRACE) as $filename){
echo $filename."</br>";
}
This returned all the files that had either en, v980 or both. I just need to figure out if there is a way to say must contain both.

PHP's glob() essentially uses the same wildcarding/matching engine as a unix shell. With your version, you're searching for files that contain en,v980, and it's not being treated as "alternation". http://www.tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm
Try
glob('..../*{en,v980}*.jar')
instead (note the {})

Related

How to correctly make conditions in TinyButStrong?

I have to make a template in the TinyButStrong language, but I have no access to the PHP side. I'm just able to modify my template and to upload it on my ERP.
Anyway, the PHP side is working well.
I tried to put an if statement in my ODT template file, but when rendering it doesn't work.
My condition:
[if [tab.product_type]!=1; then ‘[tab.product_ref]’; else ‘0’; block=table:table-row]
I verified value of tab.product_type, and the value is 0 or 1.
I tried many syntaxes, but none is working well. The only thing that it shows is:
.
Where did I make a mistake? I really don't understand, because I tried many syntaxes and I still get this line.
Little update :
I saw that the "when" is more adapted to this usage.
I found a syntax but I'm still having bad results. I made this :
[tab.product_ref;block=table:table-row;when [tab.product_type]!=1]
Anyway, it's giving me lines where tab.product_type is 1.
Why ?? I really don't understand how this language works...

extract info from jpeg with PHP

I want to extract variable lengths of information from a jpeg-file using PHP, but it is not exif-data.
If I open the jpeg with a simple text editor, I can see that the wanted informations are at the end of the file and seperated by \00.
Like this:
\00DATA\00DATA00DATA\00DATA\000\00DATA
Now if I use PHP's file_get_contents() to load the file into a string, the dividers \00 are gone and other symbols show up.
Like so:
ÿëžDATADATADATADATADATA ÿÙ
Could somebody please eplain:
Why do the \00 dividers vanish?
How to get the informations using PHP?
EDIT
The question is solved, but for those seeking a smarter solution, here is the file I try to obtain the DATA parts from: https://www.dropbox.com/s/5cwnlh2kadvi6f7/test-img.jpg?dl=0 (yes I know its corrupted)
Use instead $data = exif_read_data("PATH/some.jpg") it will give you all headers data about image, you can check its manual here - http://php.net/manual/en/function.exif-read-data.php
I came up with a solution on my own. May not be pretty, but works for me.
Using urlencode(file_get_contents()) I was able to retrieve the \00 parts as %00.
So now it reads like this:
%00DATA%00DATA%00DATA%00DATA%000%00DATA
I can split the string at the %00 parts.
I am going to accept this answer, once SO lets me do so and nobody comes up with a better solution.

Upper or Lower Case Issue using get_file_contents php

I am using a PHP GET method to grab a file name that then is placed in a get_file_contents command. If it is possible, I would like to ignore letter case so that my URL's are cleaner.
For instance, example.com/file.php?n=File-Name will work but example.com/file.php?n=file-name will not work using the code below. I feel like this should be easy but I'm coming up dry. Any thoughts?
$file = $_GET['n'];
$file_content = file_get_contents($file);
Lowercase all your filenames and use:
file_get_contents(strtolower($file));
(I hope you're aware of some of the risks involved in using this.)
The Linux filesystem is case sensitive. If you want to do case insensitive matching against files that already exist on the user's machine, your only option is to obtain a directory listing and do case-insensitive comparison.
But you don't explain where the download URLs come from. If you already know the correct filenames and you want to generate prettier URLs, you can keep a list of the true pathnames and look them up when you receive a case-normalized one in a URL (you could even rename them completely, obfuscate, etc.)

Setting language for user

I would like my php website to be able to be multilinguistic. I thought of using:
echo $lang[$_SESSION['lang']]['WellcomeMessage'];
but I found that I will be needing to format the text, say for example male/female or putting some values from the DB. So I thought that simple strings might not do the trick for formatting?
I know #define might have worked in C as the string translates to code, but I don't know how php does that. For example:
define ($lang['en']['credit_left'],'you have $credits_left');
define ($lang['sp']['credit_left'],'tienes $credits_left creditos mas');
Any suggestions?

Permanently write variables to a php file with php

I need to be able to permanently change variables in a php file using php.
I am creating a multilanguage site using codeigniter and using the language helper which stores the text in php files in variables in this format:
$lang['title'] = "Stuff";
I've been able to access the plain text of the files using fopen() etc and I it seems that I could probably locate the areas I want to edit with with regular expressions and rewrite the file once I've made the changes but it seems a bit hacky.
Is there any easy way to edit these variables permanently using php?
Cheers
If it's just an array you're dealing with, you may want to consider var_export. It will print out or return the expression in a format that's valid PHP code.
So if you had language_foo.php which contained a bunch of $lang['title'] = "Stuff"; lines, you could do something along the lines of:
include('language_foo.php');
$lang['title2'] = 'stuff2';
$data = '$lang = ' . var_export($lang, true) . ';';
file_put_contents('language_foo.php', '<?PHP ' . $data . ' ?>');
Alternatively, if you won't want to hand-edit them in the future, you should consider storing the data in a different way (such as in a database, or serialize()'d, etc etc).
It looks way easier to store data somewhere else (for instance, a database) and write a simple script to generate the *.php files, with this comment on top:
#
# THIS FILE IS AUTOGENERATED - DO NOT EDIT
#
I once faced a similar issue. I fixed it by simply adding a smarty template. The way I did it was as follows:
Read the array from the file
Add to the array
Pass the array to smarty
Loop over the array in smarty and generate the file using a template (this way you have total control, which might be missing in reg-ex)
Replace the file
Let me know if this helps.
Assuming that
You need the dictionary file in a human-readable and human-editable form (no serializing etc.)
The Dictionary array is an one-dimensional, associative array:
I would
Include() the dictionary file inside a function
Do all necessary operations on the $lang array (add words, remove words, change words)
Write the $lang array back into the file using a simple loop:
foreach ($lang as $key => $value)
fwrite ($file, "\$lang['$key'] = '$value';\n";
this is an extremely limited approach, of course. I would be really interested to see whether there is a genuine "PHP source code parser, changer and writer" around. This should be possible to do using the tokenizer functions.
If it also is about a truly multilingual site, you might enjoy looking into the gettext extension of PHP. It falls back to a library that has been in use for localizing stuff for many years, and where tools to keep up with the translation files have been around for almost quite as long. This makes supporting all the languages in later revisions of the product more fun, too.
In other news, I would not use an array but rather appropriate definitions, so that you have a file
switch ($lang) {
case 'de':
define('HELLO','Hallo.');
define('BYE','Auf wiedersehen.');
break;
case 'fr':
define('HELLO','Bonjour');
define('BYE','Au revoir.');
break;
case 'en':
default:
define ('HELLO','Hello.');
define ('BYE','Bye.');
}
And I'd also auto-generate that from a database, if maintenance becomes a hassle.
Pear Config will let you read and write PHP files containing settings using its 'PHPArray' container. I have found that the generated PHP is more readable than that from var_export()

Categories