Μηνιαία αρχεία: Απρίλιος 2016


PHP: Functions that check passwords if they are strong enough

Following you will find a function that checks a string if it matches certain limitations that are usually applied on passwords.

Specifically, in the following code we check that the input contains at least one small Latin letter, a caps Latin letter, a number and a special character.

//Function that checks if string has at least one small Latin letter, one caps Latin letter, a number and a special character
function validString($string) {
  $containsSmallLetter = preg_match('/[a-z]/', $string);
  $containsCapsLetter = preg_match('/[A-Z]/', $string);
  $containsDigit = preg_match('/\d/', $string);
  $containsSpecial = preg_match('/[^a-zA-Z\d]/', $string);
  return ($containsSmallLetter && containsCapsLetter && $containsDigit && $containsSpecial);
}

In case you do not want to check for the case of the Latin characters and you just want to see if there is any of them we can merge the two checks as follows

//Function that checks if string has at least one Latin letter, a number and a special character
function validString($string) {
  $containsLetter = preg_match('/[a-zA-Z]/', $string);
  $containsDigit = preg_match('/\d/', $string);
  $containsSpecial = preg_match('/[^a-zA-Z\d]/', $string);
  return ($containsLetter && $containsDigit && $containsSpecial);
}

Windows with ffmpeg — recursively convert all *.mov movies to .mp4. with fixed resolution for android 1

We needed to convert a bunch of mov files to mp4 and while doing that we wanted to shrink them down so that they would fit the screen of an older android device.
We did that both to save space on the internal memory and to make the device perform as efficient as possible as it would not have to shrink the video on the fly.

We downloaded the windows binary for ffmpeg from https://ffmpeg.org/ and copied it to the folder we wanted to execute the command from using Windows Explorer.
After that, while holding the Shift key we right clicked in the Windows Explorer empty area to popup the menu. From the menu we selected Open command window here, that opened a Command Prompt that was already navigated in the folder we placed the binary.
To convert the movies we executed the following:

for /R %f in ("*.mov") do (ffmpeg.exe -i "%~f" -s 864x486 -acodec copy "%~pf%~nf.mp4")

What the above command did was, direct command prompt to find recursively all the files that their name ends in .mov (this is the part that looks like this for /R %f in ("*.mov")) and then execute for each a command, in our case was to convert the file to mp4, resize the video while preserving the audio as is and produce a new file that has the same name but different file extension so that new files will have the mp4 extension instead of mov.


Bash: Show GIT Remote Origin for each immediate subfolder

To print on screen all the immediate subfolders and their GIT Remote Origin URL configuration we used the following command

find . -maxdepth 1 -type d \( ! -name . \) -exec bash -c "cd '{}' && echo '{}' && git config --get remote.origin.url" \;

We used the find general command to get all folders that are in depth 1, in other words all folders that are in the specific folder where we started the search.
In our case we used the dot as the starting folder which means that it will run the find in the same folder as we were navigating in.
We passed as a parameter the -type d to instruct find to show only folders and ignore the files.
The \( ! -name . \) prevents executing the command in current directory by removing it from the result set.
With the results we executed some commands on each.

Specifically, we created a new bash session for each result that navigated in the folder, printed out the name of the matched folder and then print the Remote Origin URL using the command git config --get remote.origin.url


Grep: Print only the words of the line that matched the regular expression, one per line

grep -oh "\w*$YOUR_PATTERN\w*" *

We used the following parameters on our command:

-h, –no-filename : Suppress the prefixing of file names on output. This is the default when there is only  one  file  (or only standard input) to search.
-o, –only-matching : Print  only  the matched (non-empty) parts of a matching line,  with each such part on a separate output line.

Also, we wrapped out pattern with the \w* that matches  all word-constituent characters on either side. The * character states that it should find 0 or more of those characters in the pattern to match.