PHP

cURL

Is cURL installed on my machine

apt-get install php5-curl

sudo service apache2 restart

 

Create a MySQLi connection


/*
 * Create a MySQLi connection 
 * and fetch data from a table.
 */

// Set host and credentials.
$host = '127.0.0.1';
$database = 'db';
$user = 'user';
$password = 'pass';

// Create and check connection.
$connection = mysqli_connect($host, $user, $password, $database);
if(!$connection)
  exit('Could not establish connection ' . mysqli_connect_error());

// Create a simple query.
$items = $connection->query('SELECT * FROM table');

// See number of items to handle.
print 'Number of items fetched: ' . $items->num_rows;

Drop MongoDB database in PHP


// Establish connection.
$connection = new Mongo();

// Select database.
$database = $connection->demo;

// Drop database.
$dropped = $database->drop();
var_dump($dropped);

// Close connection.
$connection->close();

First steps using MongoDB in PHP


// Establish connection.
$connection = new Mongo();
// Select database, create database if database not exists.
$database = $connection->demo;
// Select collection.
$collection = $database->users;
// Insert data.
$user = array(
  'name' => 'Dribbit',
  'email' => 'info@dribbit.eu',
);
$collection->insert($user);

// Get all records.
$records = $collection->find();
var_dump($records);
foreach($records as $record) {
  var_dump($record);
}

// Close connection.
$connection->close();

Find files with a pattern in a directory


define('DIR_PATH', 'DIRECTORY PATH');

$filenames = scandir(DIR_PATH);

$pattern = '/^PATTERN/';
$matches = array();
$matches[] = preg_grep($pattern, $filenames);

print_r($matches);

A class used to read CSV files


/**
 * CsvProcessor.
 * 
 * @author Dribbit info@dribbit.eu
 *
 */
class CsvProcessor {

  /**
   * Process csv files and return as an associated or indexed array
   * depending if headers are set.
   * 
   * @param $file
   *  String File path.
   * @param $length
   * Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters). 
   * By setting to 0, the maximum line length is not limited, which is slightly slower. 
   * @param $delimeter
   *  String containing the CSV delimeter.

A class used to read textfiles


/**
 * TextProcessor.
 * 
 * @author Dribbit info@dribbit.eu
 *
 */
class TextProcessor {

  /**
   * Process textfiles and return as an associated array.
   * 
   * @param $file
   *  String File path.
   * @param $length
   * Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), 
   * or on EOF (whichever comes first). If no length is specified, 
   * it will keep reading from the stream until it reaches the end of the line. 
   *  
   * @return array
   * 
   */

Combine array values into a string


// Testing variable to check conditions.
$check = false;

$colors = array('red', 'white', 'green');
$sizes = array('small', 'medium', 'large');
$additionals = array('print', 'no print');
$combinations = array();

foreach($colors as $color) {
  foreach($sizes as $size) {
    // Add third level depending on the $check variable.
    if($check === true) {	
  	  foreach($additionals as $additional) {
	    $combinations[] = $color . '-' . $size . '-' . $additional;
	  }
	} else {
	  $combinations[] = $color . '-' . $size;	
	}
  }
}

print_r($combinations);
Subscribe to RSS - PHP