Multi dimension arrays

php No Comments »

$colors=array(
  array("red","rgb(255,0,0)"),
  array("yellow","rgb(255,255,0)"),
  array("green","rgb(0,255,0)"),
  array("blue","rgb(0,0,255)")
  );
echo $colors[0][0];
echo " - ";
echo $colors[0][1];
echo "<br/>";

This really makes clear to us that what we are creating is in fact an array of arrays.
The output is: red - rgb(255,0,0)

shell_exec() example

php No Comments »

$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";

Add ‘2>&1′ to the end of your shell command to have STDERR returned as well as STDOUT.

$shell_return = shell_exec($shell_command." 2>&1");

Array union example

php No Comments »


$a = array(1,2,3);
$b = array(1,7,8,9,10);
$c = $a + $b; // Union of $a and $b
echo "Union of \$a and \$b: \n";
print_r($c);

Output:
Union of arrays $a and $b:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 9 [4] => 10 )

Roman number conversion

php No Comments »

function roman ($nr ) {
     $base_digits= array (
           1=> "I",
           10=> "X",
           100=> "C",
           1000=> "M",
           );
     $help_digits= array (
           5=> "V",
           50=> "L",
           500=> "D",
           );
     $all_digits= $base_digits+ $help_digits;
     foreach ($base_digits as $key1=> $value1 )
           foreach ($all_digits as $key2=> $value2 )
                 if ($key1< $key2 )
                       $segments[$key2- $key1 ]= $value1. $value2;
     $segments+= $all_digits;
     krsort ($segments );
     foreach ($segments as $key=> $value )
           while ($key<= $nr ) {
                 $nr-= $key;
                 $str.= $value;
                 }
     return $str;
     }

echo roman (888);  //  prints DCCCLXXXVIII

Some php code examples with strings

php No Comments »

How to get the first character of a string:

$str = 'This is a test.';
$first = $str{0};

Get the third character of a string:

$third = $str{2};

Get the last character of a string:

$str = 'This is still a test.';
$last = $str{strlen($str)-1};

Modify the last character of a string:

$str = 'Look at the sea';
$str{strlen($str)-1} = 'e';

Explode CSV (MS Excel) string

php No Comments »

function csv_explode($str, $delim = ',', $qual = "\"")
{
   $len = strlen($str);
   $inside = false;
   $word = '';
   for ($i = 0; $i < $len; ++$i) {
       if ($str[$i]==$delim && !$inside) {
           $out[] = $word;
           $word = '';
       } else if ($inside && $str[$i]==$qual &&
                  ($i<$len && $str[$i+1]==$qual)) {
           $word .= $qual;
           ++$i;
       } else if ($str[$i] == $qual) {
           $inside = !$inside;
       } else {
           $word .= $str[$i];
       }
   }
   $out[] = $word;
   return $out;
}   

// Test...
$csv_str = 'a,"""","""",d,e,f';
print_r( csv_explode($csv_str) );

Very quick way to get a file extenstion

php No Comments »

The array_pop() function will push the last element of the array in the assigned varable (ie $file_ext) in this case.

$file_ext = array_pop(explode(”.”,$real_filename));

How to split a text file into lines using explode()

php No Comments »

Here’s how to split a text file into lines using explode(). This could be very useful for restoring backed up databases, when you need to pass every line (SQL statement) to MYSQL separatly:

$theFile = file_get_contents('file.txt');
$lines = array();
$lines = explode("\n", $theFile);
$lineCount = count($lines);
for ($i = 0; $i < $lineCount; $i++){
  echo $lines[$i]."<hr>";
} //for

Better last word function

php No Comments »


function lastword($theString)
{
$stringParts = explode(" ", $theString);
return array_pop($stringParts);
}

How to get the filename from an URL

php No Comments »


$url = 'http://www.site.com/images/logo.gif';
$x = explode('/', $url);
$filename = trim($x[count($x)-1]);
echo $filename;

The code output will be “logo.gif”

WebSite Powered by webHauser
Entries RSS Comments RSS Login