Category Archives: Notes

General Notes, normally things I discover as I work.

PHP Search and Replace Directory Recursively

On a message forum I regularily visit a member was having issues with code being placed in multiple files that he wanted to remove or change without having to edit each and every file manually. For this I created a search and replace function which will recursively search the directory and every file Continue reading PHP Search and Replace Directory Recursively

[PHP] Get current time in milliseconds

I recently needed to get the current time in milliseconds, of course this can easily be retrieved using microtime() however I needed the entire number in digits, not in decimals with seconds seperately etc. Heres how:

1
2
3
$timeparts = explode(" ",microtime());
$currenttime = bcadd(($timeparts[0]*1000),bcmul($timeparts[1],1000));
echo $currenttime;

NOTE: PHP5 is required for this function due to the improvements with microtime() and the bc math module is also required (as we’re dealing with large numbers, you can check if you have the module in phpinfo).

Bash, Bad Interpreter

I recently came across:

/bin/bash^M: bad interpreter: No such file or directory

When trying to run a bash script that someone else had edited. This most commonly happens on UNIX systems if the file has been edited on MS-DOS or Mac. The cure for this is to run a simple perl script:

1
2
#!/usr/bin/perl -pi
s/\n/\r\n/;

Save as unix2dos.pl, chmod +x unix2dos.pl and then run like:

./unix2dos.pl filetoconvert.txt

Simples!