Handy string manipulation functions

Many people like to keep their code tidy, even the final HTML that you can see when selecting "Show source" option in your browser. This includes having correct number of indents for all levels.

It would make the code cluttered and messy if we had to define that we wanted so many tabs to be prepended or a new line character appended (like many often do - $text . "\n").

Here are two functions to avoid that - printLn and sprintLn - they print the text and return the text respectively.

Remove values from an array

Prototype.js library has a handy function called Array.without, which "produces a new version of the array that does not contain any of the specified values.".

Quite often one might need to do the same thing in PHP. Here is a function for those occasions.

Convert size in bytes to a human readable format (PHP)

Another day and another function. This time a function, which converts size in bytes to a human readable format. For example, 1024 will become "1KB", but 1048576 will become "1MB".

You can also specify the precision that you want the number to formated with. For example, if $bytes variable was originally 1,000,000 bytes then the result would be "976.56 KB" if the precision set to 2 or "976 KB" if set to 0.

Ensure that the string ends with a specific character

From my times in Delphi I remember three functions (or routines) which were quite useful in working with directories and paths. They both are located in Delphi's SysUtils unit and are called IncludeTrailingBackslash and IncludeTrailingPathDelimiter.

Lately I was doing directory-related tasks and I decided that these functions would come in handy if I had them in PHP so I decided to create my own ports of them.

Generate a random GUID

Function generateGuid creates a random GUID (globally unique identifier).

MSDN defines GUID as "a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated."

GUID consists of alphanumeric characters only and is grouped in five groups separated by hyphens as seen in this example:
3F2504E0-4F89-11D3-9A0C-0305E82C3301

Showing 21 - 25 of 32 results