Skip to content

Instantly share code, notes, and snippets.

@jgdoncel
jgdoncel / bom.php
Created May 30, 2014 16:11
Detectar y eliminar el BOM de un fichero de texto
// Comprobar si el fichero trae BOM
$bom = pack("CCC", 0xef, 0xbb, 0xbf);
if (0 == strncmp($json, $bom, 3)) {
$json = substr($json, 3);
}
function deleteDirectoryRecursive($dirPath) {
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) {
$path->isDir() ? rmdir($path->getPathname()) : unlink($path->getPathname());
}
rmdir($dirPath);
}
@jgdoncel
jgdoncel / partition_by_month.sql
Last active August 29, 2015 14:22
Particionar tabla Ejemplo suponiendo una tabla test (id,fecha) que pretendemos particionar por meses. Conviene crear el campo para el HASH debido a que MySQL filtra las consultas por particiones siempre que se incluya el HASH en el filtro. https://dev.mysql.com/doc/refman/5.6/en/partitioning-pruning.html
-- Crear nuevo campo
ALTER TABLE `test` ADD COLUMN `ts_year_month` int(11);
-- Insertar valores en el campo para los registros actuales
UPDATE `test` SET ts_year_month = DATE_FORMAT(fecha,'%Y%m');
-- Actualizar la definición de la nueva columna
ALTER TABLE `test` MODIFY `ts_year_month` int(11) NOT NULL;
-- Añadir el nuevo campo al índice
@jgdoncel
jgdoncel / gist:2635022
Created May 8, 2012 13:32
MySQL INSERT o UPDATE si existe
INSERT INTO clientes_peticiones (
id_cliente,fecha,peticiones
) VALUES (
1,'2011-11-02',1
) ON DUPLICATE KEY UPDATE peticiones = peticiones+1;
@jgdoncel
jgdoncel / utf8.php
Created October 2, 2013 13:35
Eliminar caracteres no UTF8 de una cadena
// Supposing your want to be sure that $string is composed by valid UTF-8 characters:
$string = iconv("UTF-8","UTF-8//IGNORE",$string);
// If you want to remove none ISO-8859-1 characters from this string:
$string = iconv("UTF-8","ISO-8859-1//IGNORE",$string);
$string = iconv("ISO-8859-1","UTF-8",$string);
// If you still have characters you want to be ignored, try this:
$string = recode_string("us..flat", $string);
@jgdoncel
jgdoncel / ejemplo_01.sql
Created October 2, 2013 13:44
Numerar filas en una consulta
# La siguiente consulta añade un campo numerado a una consulta:
set @num=0;
select id, @num:=@num+1 AS contando FROM tabla;
@jgdoncel
jgdoncel / combine_folder.bat
Created October 2, 2013 13:45
Combinar archivos
REM Combinar los archivos js de un directorio
@echo off
pushd "%~1"
echo.>tmp.js
del tmp.js
for /r %%x in (*.js) do (
type "%%~x" >> tmp.js
)
popd
@jgdoncel
jgdoncel / order.sql
Created October 2, 2013 13:50
Ordenar en base a un campo En el ejemplo se ordenan las líneas de pedido en su orden natural pero manteniendo unidas las que son del mismo color.
Select * FROM pedidos_lineas
where id_pedido=33
order by find_in_set(color, (SELECT GROUP_CONCAT(DISTINCT(color)) FROM pedidos_lineas WHERE id_pedido=33));
@jgdoncel
jgdoncel / update.sql
Last active December 24, 2015 11:58
Ejemplo de actualización de un campo de un tabla tomando el valor de un campo de una tabla diferente
UPDATE Hoja1 origen, clientes_acceso destino
SET destino.password = origen.NUEVA_CLAVE
WHERE destino.IDusuario = origen.CODIGO;
@jgdoncel
jgdoncel / .htaccess
Created October 2, 2013 21:46 — forked from pbougie/.htaccess
Set time zone to Coordinated Universal Time (UTC) for PHP in an .htaccess file.
php_value date.timezone UTC