Skip to content

Instantly share code, notes, and snippets.

View alphp's full-sized avatar

Fernando Herrero alphp

View GitHub Profile
@alphp
alphp / SetColor.php
Created February 23, 2018 22:48
New implementation of SetDrawColor, SetFillColor and SetTextColor for FPDF
<?php
function SetDrawColor() {
$args = func_get_args();
if (count($args) and is_array($args[0])) {
$args = $args[0];
}
switch (count($args)) {
case 1:
$this->DrawColor = sprintf('%.3f G', $args[0] / 100);
@alphp
alphp / AppController.php
Last active February 16, 2018 07:15
Function for list all Controllers and Methods for CakePHP 2.x
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
protected function get_controllers () {
$controllers = null;
$app_controller = get_class_methods('AppController');
$controller_classes = App::objects('controller');
foreach ($controller_classes as $controller_class) {
@alphp
alphp / path_remove.php
Last active August 27, 2018 04:12
Remove directory (PHP)
<?php
function rd ($path) {
if (is_file($path)) {
unlink($path);
} else {
array_map('rd', glob($path . DIRECTORY_SEPARATOR . '*'));
rmdir($path);
}
}
@alphp
alphp / cif_validation.php
Last active May 12, 2022 08:53
Spanish CIF validation (PHP)
<?php
function cif_validation ($cif) {
$cif = strtoupper($cif);
if (preg_match('~(^[XYZ\d]\d{7})([TRWAGMYFPDXBNJZSQVHLCKE]$)~', $cif, $parts)) {
$control = 'TRWAGMYFPDXBNJZSQVHLCKE';
$nie = array('X', 'Y', 'Z');
$parts[1] = str_replace(array_values($nie), array_keys($nie), $parts[1]);
$cheksum = substr($control, $parts[1] % 23, 1);
return ($parts[2] == $cheksum);
} elseif (preg_match('~(^[ABCDEFGHIJKLMUV])(\d{7})(\d$)~', $cif, $parts)) {
@alphp
alphp / path_check.php
Last active August 27, 2018 04:12
Check if path exists (PHP)
<?php
function path_check ($path, $mode = null) {
if (!is_dir($path)) {
if (!empty($mode)) {
if (is_file($path)) {
unlink($path);
}
return mkdir($path, $mode, true);
}
return false;
@alphp
alphp / path_rename.php
Last active August 27, 2018 04:12
Rename a path (PHP)
<?php
require('path_check.php');
function path_rename ($from, $to) {
if (is_dir($from)) {
path_check($to, 0777);
foreach (glob($from . DIRECTORY_SEPARATOR . '*') as $item) {
path_rename($item, $to . DIRECTORY_SEPARATOR . basename($item));
}
rmdir($from);