Category Archives: php

Convert Hex Color to/from RGB

Function to convert Hex Color to RGB

function hex2rgb($hex) {
   $hex = str_replace("#", "", $hex);

   if(strlen($hex) == 3) {
      $r = hexdec(substr($hex,0,1).substr($hex,0,1));
      $g = hexdec(substr($hex,1,1).substr($hex,1,1));
      $b = hexdec(substr($hex,2,1).substr($hex,2,1));
   } else {
      $r = hexdec(substr($hex,0,2));
      $g = hexdec(substr($hex,2,2));
      $b = hexdec(substr($hex,4,2));
   $rgb = array($r, $g, $b);
   //return implode(",", $rgb); // returns the rgb values separated by commas
   return $rgb; // returns an array with the rgb values
$rgb = hex2rgb("#cc0");
Array ( [0] => 204 [1] => 204 [2] => 0 )

Function to convert RGB color to Hex

function rgb2hex($rgb) {
   $hex = "#";
   $hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
   $hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
   $hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);

   return $hex; // returns the hex value including the number sign (#)
$rgb = array( 255, 255, 255 );
$hex = rgb2hex($rgb);
echo $hex;

Javascript: URL Validation

Javascript: url validating function

[code language="javascript"]

function isUrl(s) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(s);


[code language="javascript"]
if (isUrl(""))
alert("is correct")
alert("not correct");

How to redirect iPone users using PHP

// Redirect iPhone/iPod visitors
if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') ||
   strpos($_SERVER['HTTP_USER_AGENT'], 'iPod')){

User agent string for reference

User Agent String for iPhone
    Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko)
    Version/3.0 Mobile/1A543a Safari/419.3
User Agent String for iPod Touch
    Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko)
    Version/3.0 Mobile/3A101a Safari/419.3

Info: To look into

while (list($key,$value)=each($parameters_array))

handy little bit of code I wrote to take arguments from the command line and parse them for use in my apps.


$i = implode(" ",$argv); //implode all the settings sent via clie
$e = explode("-",$i); // no lets explode it using our defined seperator ‘-’

//now lets parse the array and return the parameter name and its setting
// since the input is being sent by the user via the command line
//we will use stristr since we don’t care about case sensitivity and
//will convert them as needed later.

while (list($index,$value) = each($e)){

//lets grap the parameter name first using a double reverse string
// to get the begining of the string in the array then reverse it again
// to set it back. we will also "trim" off the "=" sign

$param = rtrim(strrev(stristr(strrev($value),’=')),"=");

//now lets get what the parameter is set to.
// again "trimming" off the = sign

$setting = ltrim(stristr($value,’='),"=");

// now do something with our results.
// let’s just echo them out so we can see that everything is working

echo "Array index is ".$index." and value is ".$value."\r\n";
echo "Parameter is ".$param." and is set to ".$setting."\r\n\r\n";



when run from the CLI this script returns the following.

[root@fedora4 ~]# php a.php -val1=one -val2=two -val3=three

Array index is 0 and value is a.php
Parameter is and is set to

Array index is 1 and value is val1=one
Parameter is val1 and is set to one

Array index is 2 and value is val2=two
Parameter is val2 and is set to two

Array index is 3 and value is val3=three
Parameter is val3 and is set to three

List of string functions in php

All String Functions in PHP
str_replace: How to replace a part of a string with another string
str_ireplace: Case in-sensitive search and replace using array of strings
strlen: How to find length of a string in PHP?
trim: Removing empty space from both sides of a string
strrev: Reversing a string by using strrev function in PHP
Adding two or more strings in PHP
stristr: Searching for a presence of a string inside another string
nl2br: Adding Line breaks inside a string in place of carriage returns
split: Breaking a string to form array using delimiters
substr: Collecting part of a string
substr_count: Counting occurrence of sub string in main string
str_repeat: Repeating a string number of times
strtolower(): function to change alphabets to Lower case
strtoupper(): function to change characters to upper case letters
function to collect part of a string with two landmarks
Random string generator with number and alphabets for password
strcasecmp(): Case insensitive string comparison
strcmp(): Case sensitive string comparison
str_pad(): String pad with specified length
md5 hash of a string: encryption of a string
Separating domain and userid part from an email address using split
htmlspecialchars: Printing html special chars to the page
strip_tags: Removing html tags within a string
ucwords: Converting first letter of every word to UpperCase

php Code to analyze – ini_set usage

php Code to Analyze

ini_set usage with “include_path”

if ( function_to_test($var ) ) ini_set("include_path", "/var/www/vhosts/");
else ini_set("include_path", "/var/www/vhosts/");

require "modules/mpgClasses.php";
include "modules/mpiClasses.php";

PHP: What does it mean ($$)?

The code and output below.
What’s just happend?

$tst = &quot;hello&quot;;
$$tst = &quot;test value&quot;;

var_dump( $$tst );
var_dump( $tst );
var_dump ( $hello) ;

string(10) "test value" string(5) "hello" string(10) "test value"