This function was inspired from PHP’s built-in function trim()
that removes undesired characters from the start and end of a string, and in case no such characters are provided as second argument to the function, it removes white-space characters from both ends of the string.
Implementing QuickSort in PHP
Quicksort is practically the fastest way to sort an array
of data. PHP’s array
sorting function sort()
uses QuickSort.
Replace last occurrence of a String – PHP
Replacing part of a string with another string is straight forward, but what if you have to replace last occurrence of a character or string with another string.
Getting Red-Green-Blue from Hexadecimal Colors – PHP Bitwise
When working with CSS, it is common to use Hexadecimal Notation of colors, while on the back end, many PHP functions (like imagecolorallocate()
, imagecolorclosest()
, imagecolorexact()
etc, require that you pass on colors as red, green and blue integers.
Implementing logical right shifting in PHP
PHP’s bitwise operators <<
and >>
implement Arithmetic Bit Shifting. This means:
- Left shift (using
<<
operator) fills in zero(s) on the right end of the bitfield, and bit(s) shifted off the left end are discarded; - Right shift (using
>>
operator) discards bit(s) shifted off the right end of the bitfield and copy(ies) of sign bit are shifted in on the left end.
Logical Bit Shifting is different in that on right shifts, zeros are shifted in on the left end of the bitfield, affecting the sign of negative integer.
Finding Even or Odd numbers with PHP
PHP does not provide any mathematical function like is_even
to help you check if a given integer is an Even
number or Odd
. Writing your own such function is not difficult and below are listed a number of alternatives that provide different logical solutions to do this.