Friday, January 29, 2016

PHP: Clean-up Web Form Data

PHP


function clean_input($a){

    $data=trim($a);
    $data=stripslashes($a);
    $data=htmlspecialchars($a);
    
    return $data;
}


if($_SERVER['REQUEST_METHOD'] == 'POST'){

    $name=clean_input($_POST['name']);
    $phone=clean_input($_POST['phone']);
    $email=clean_input($_POST['email']);
    $comments=clean_input($_POST['comments']);
    
}

PHP: Determining if substring is in position zero.

PHP


$var="Duis posuere nunc sit amet sapien pharetra, at scelerisque risus fringilla. Aenean sed purus nec quam ullamcorper aliquam.";




function sub1($var,$sub){
   if(strpos($var,$sub) === 0){ // use identity operator because false is also equal to 0
     echo "<p>".str_pad($sub,25,".").str_pad(" is in position zero",25,".",STR_PAD_LEFT)."</p>";
   }
   else{
     echo "<p>".str_pad($sub,25,".").str_pad(" is not in position zero",25,".",STR_PAD_LEFT)."</p>";
   }
}




sub1($var,'scelerisque');

sub1($var,'Duis');

//Result

scelerisque............... is not in position zero
Duis.......................... is in position zero

Thursday, January 28, 2016

PHP: Fun with string functions

PHP


$var="Duis posuere nunc sit amet sapien pharetra, at scelerisque risus fringilla. Aenean sed purus nec quam ullamcorper aliquam.";

printf("%s %s","String equals:",$var);

echo "<pre>"; 

echo "<p><b>Substring: substr(\$var,12,9): ' nunc sit'</b></p>";

echo "<p>".substr($var,12,9)."</p>";

echo "<p><b>Substring and Subpoition: substr(\$var,27,6).' => '.strpos(\$var,'sapien'): sapien => 27</b></p>";

echo "<p>".substr($var,27,6)." => ".strpos($var,"sapien")."</p>";
echo "</pre>";

echo "<p><b>explode string and list</b></p>";

$arr=explode(" ",$var);

foreach($arr as $val){
  echo "<p>".$val."</p>";
}  


echo "<p><b>explode string to array and implode back to string (just because)</b></p>";
echo "<pre>";

echo "<p>".implode(" ",explode(" ",$var))."</p>";

echo "</pre>";
  

//Result


String equals: Duis posuere nunc sit amet sapien pharetra, at scelerisque risus fringilla. Aenean sed purus nec quam ullamcorper aliquam.
Substring: substr($var,12,9): ' nunc sit'
nunc sit
Substring and Subpoition: substr($var,27,6).' => '.strpos($var,'sapien'): sapien => 27
sapien => 27
explode string and list
Duis
posuere
nunc
sit
amet
sapien
pharetra,
at
scelerisque
risus
fringilla.
Aenean
sed
purus
nec
quam
ullamcorper
aliquam.
explode string to array and implode back to string (just because)
Duis posuere nunc sit amet sapien pharetra, at scelerisque risus fringilla. Aenean sed purus nec quam ullamcorper aliquam.

PHP: parse_url()

PHP


$url=parse_url("http://www.amazon.com/s/ref=nb_sb_noss_2/188-0127043-7511934?url=search-alias%3Daps&field-keywords=camera");

print_r($url);
//Result 

Array ( [scheme] => http [host] => www.amazon.com [path] => /s/ref=nb_sb_noss_2/188-0127043-7511934 [query] => url=search-alias%3Daps&field-keywords=camera )

Wednesday, January 27, 2016

PHP: Using str_pad() to format text

PHP: Book List


$header="BOOK LIST";

$arr=array(
  'AYN RAND'=> 'Calumet K', 
  'ERNEST HEMINGWAY' =>' Wuthering Heights',
  'JOAN DIDION'=> 'Victory', 
  'RAY BRADBURY' => 'Fahrenheit 451 ',
  'GEORGE R.R. MARTIN' => 'Game of Thrones',
  'MARK TWAIN' => 'The Adventures of Tom Sawyer',
  'Stephen King' => 'The Shining',
  'James Patterson' => 'Alex Cross',
  'C. S. Lewis' => 'The Chronicles of Narnia',
  'Edgar Rice Burroughs' => 'Tarzan',
);

echo "<p>";
  
print(str_pad($header,60,"/",STR_PAD_BOTH)); //60 chars across: pad on both sides
  
echo "</p>";

foreach($arr as $author => $book){
  echo "<p>";
  print(str_pad($author,30,'.')); //30 chars across: default pad right side
  print(str_pad($book,30,'.',STR_PAD_LEFT)); //30 chars across: pad on left side
  echo "</p>";
  
} 

//Result

/////////////////////////BOOK LIST//////////////////////////
AYN RAND...........................................Calumet K
ERNEST HEMINGWAY.......................... Wuthering Heights
JOAN DIDION..........................................Victory
RAY BRADBURY.................................Fahrenheit 451
GEORGE R.R. MARTIN...........................Game of Thrones
MARK TWAIN......................The Adventures of Tom Sawyer
Stephen King.....................................The Shining
James Patterson...................................Alex Cross
C. S. Lewis.........................The Chronicles of Narnia
Edgar Rice Burroughs..................................Tarzan

Tuesday, January 26, 2016

PHP: usort() custom sorting

PHP
function user_compare($x, $y)
{
if ($x == $y)
return 0;
else if ($x > $y)
return 1;
else
return -1;
}
$array1 = array(22,33,66,55,11);
usort($array1, 'user_compare');
foreach($array1 as $key => $value)
{
echo "$key: $value\n";
}

PHP: foreach() loop

PHP: Circumference of planets table with alternating rows


$rad=array(
  'mercury' => 1516,
  'venus' => 3760,
  'earth' => 3959,
  'mars' => 2104,
  'jupiter' => 42982,
  'saturn' => 35615,
  'uranus' => 15700,
  'neptune' => 15256,
);

echo "<table border='1' width='300' >";
echo "<tr><td>Planet</td><td>Radius (mi)</td><td>Circumference (mi)</td></tr>";
foreach($rad as $key => $val){
  $cir=(2*M_PI)*$rad[$key];
  if($key == 'venus' || $key == 'mars' || $key == 'saturn' || $key == 'neptune'){
      echo "<tr><td><b>";
      printf("%s", $key);
      echo "</b></td>";
      echo "<td><b>";
      printf("%.2f", $val);
       echo "</b></td>";
     echo "<td><b>";
      printf("%.2f",$cir);
      echo "</b></td></tr>";
     
   }
  else{
      echo "<tr><td>";
      printf("%s", $key);
      echo "</td>";
      echo "<td>";
      printf("%.2f", $val);
      echo "</td>";
      echo "<td>";
      printf("%.2f",$cir);
      echo "</td></tr>";
     
    }

}

echo "</table>";




//Result

PlanetRadius (mi)Circumference (mi)
mercury1516.009525.31
venus3760.0023624.78
earth3959.0024875.13
mars2104.0013219.82
jupiter42982.00270063.87
saturn35615.00223775.64
uranus15700.0098646.01
neptune15256.0095856.28