PHP MISC

Standard

dir()
returns an instance of the directory class
$dir = “.”;
$handle = opendir($dir);

// or create a directory instance from the class
$d = dir(“.”);
// accessing the properties of the directory class
echo “handle: ” . $d->handle . “<br>”;
echo “path: ” . $d->path . “<br>”;
while (($file = $d->read()) !== false){
echo “filename: ” . $file . “<br>”;
}
$d->close();

// outputs
Handle: Resource id #2
Path: /etc/php
filename: .
filename: ..
filename: ajax.gif
filename: books.xml
filename: cdcatalog.xml
filename: default.asp

## instances of directory are created by the dir() function, not by the new operator ##
Directory {
## properties ##
public string $path ;
public resource $handle ;
## methods ##
public void close ([ resource $dir_handle ] )
public string read ([ resource $dir_handle ] )
public void rewind ([ resource $dir_handle ] )
}

Directory::read
read entry from directory handle

the same is readdir(), only dir_handle defaults to $this: returns the name of the next entry in the directory and the entries are returned in the order in which they are stored by the filesystem

getcwd()
returns the current working directory
echo getcwd()
// print /home/desktop

opendir(path)
opens a directory handle, where path is the directory path to be opened
$dir = “/images/”;
// open a directory and read its contents
if (is_dir($dir)){
if ($handle = opendir($dir)){
while (($file = readdir($handle)) !== false){
echo “filename:” . $file . “<br>”;
}
closedir($handle);
}
}

// outputs
filename: dog.txt
filename: jack.txt
filename: russel.txt

returns the directory handle resource on success
false on failure, throwing an error of level E_WARNING if path is not a valid directory, or if the directory cannot be opened due to permission restrictions or filesysytem errors
hide the error output of opendir() by adding @ to the front of the function name

readdir(dir_handle)
returns an entry name (filename) from a directory handle on success
the directory handle resource previously opened with opendir() can be specified as a parameter
if this parameter is not specified, the last link opened by opendir() is assumed
returns false on failure

define()
defines a constant
define(name, value, case_insensitive)
the case_insensitive option can be setted to true or false

dirname()
return the directory name component from a path
echo dirname(“c:/demo/index.php”);
// outputs c:/demo

basename()
return the file name component from a path
basename(path, suffix)
the suffix option specifies a file extension: if the filename has this file extension, the file extension will not show

echo basename(“c:/demo/index.php”);
// outputs index.php

echo basename(“c:/demo/index.php”, “.php”);
// outputs index

Advertisements

DETECT IE VERSION

Standard

JAVASCRIPT CODE
// get IE or EDGE browser version
var version = detectIE();

if (version === false) {
document.getElementById(‘result’).innerHTML = ‘IE/Edge‘;
} else if (version >= 12) {
document.getElementById(‘result’).innerHTML = ‘Edge ‘ + version;
} else {
document.getElementById(‘result’).innerHTML = ‘IE ‘ + version;
}

// add details to debug result
document.getElementById(‘details’).innerHTML = window.navigator.userAgent;

/**
* detect IE
* returns version of IE or false, if browser is not internet explorer
*/
function detectIE() {
var ua = window.navigator.userAgent;

// Test values; uncomment to check result …

// IE 10
// ua = ‘Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)’;

// IE 11
// ua = ‘Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko’;

// EDGE 12 (SPARTAN)
// ua = ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0’;

// EDGE 13
// ua = ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586’;

var msie = ua.indexOf(‘MSIE ‘);
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf(‘.’, msie)), 10);
}

var trident = ua.indexOf(‘Trident/’);
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf(‘rv:’);
return parseInt(ua.substring(rv + 3, ua.indexOf(‘.’, rv)), 10);
}

var edge = ua.indexOf(‘Edge/’);
if (edge > 0) {
// Edge (IE 12+) => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf(‘.’, edge)), 10);
}

// other browser
return false;
}

CSS CODE
@import url(https://fonts.googleapis.com/css?family=Fira+Mono|Fira+Sans:300);

body {
color: black;
background-color: white;
font-family: “Fira Sans”, sans-serif;
font-weight: 300;
margin: 0;
padding: 3rem;
}

h1 {
color: darkgrey;
text-align: center;
font-weight: 300;
font-size: 1.5rem;
line-height: 2rem;
}

h2 {
text-align: center;
font-weight: 300;
font-size: 4rem;
}

p {
color: darkgrey;
text-align: center;
font-family: “Fira Mono”, monospace;
font-size: 1rem;
line-height: 1.5rem;
}

HTML CODE
< h1 > detect IE/EDGE version with JAVASCRIPT </ h1 >
< h2 id=”result” > detecting </ h2 >
< p id=”details” > n/a </ p >

HTML BUTTON ELEMENT

Standard

HTML button definition
< button type=”button” > I’m a button </button >

CSS code to define the button’s style
button{
width:200px;
height:35px;
background-color: transparent;
color: #fff;
font-family: Helvetica, Arial, sans-serif;
font-size: 14px;
background-image: url(“../images/button_bg.png”);
border:0;
}

CSS code to create a rollover effect (changement at mouse hover)
button:hover{
background-image: url(“../images/button_hover.png”);
}

CSS code to create an effect at the click
button:active{
background-image: url(“../images/button_active.png”);
}

button script creation
to show a message at the button click, use the onclick attribute with a little piece of javascript code (javascript in-line)

< button type=”button” onclick=”alert(‘hello’)” > I’m a button </button >

HTML SPAN ELEMENT

Standard

< p > my grandma has < span style=”color:blue” > blue </ span > eyes </ p >

l’elemento span viene usato per raggruppare elementi in-line
da solo non porta alcun cambiamento concreto, ma con degli attributi permette di differenziare una parte di testo da un’altra