Skip to content

Instantly share code, notes, and snippets.

@jiglesiasabio
Last active December 28, 2015 23:19
Show Gist options
  • Save jiglesiasabio/7577892 to your computer and use it in GitHub Desktop.
Save jiglesiasabio/7577892 to your computer and use it in GitHub Desktop.
General SwissKnife - some handy snippets

Validating time string (hh:mm)

$timeIsOK = preg_match('#^([01]?[0-9]|2[0-3]):[0-5][0-9]?$#', $timeString);

src: http://stackoverflow.com/questions/11005728/php-validating-24-hour-time-format

Validating time string (hh:mm:ss)

$timeIsOK = preg_match('#^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', $timeString);

src: http://stackoverflow.com/questions/11005728/php-validating-24-hour-time-format

Validating date string (dd/mm/yyyy)

$dd = $mm = $yyyy = "";
list($dd,$mm,$yyyy) = explode('/',$dateString);
$dateOK = checkdate($mm,$dd,$yyyy);

src: http://stackoverflow.com/a/3721084

Pretty PHP var_dump & print_r

//For print_r:
echo "<pre>", print_r($data, 1), "</pre>";
//For var_dump():
echo "<pre>", var_dump($data), "</pre>";

Convert query to tree (categories & subcategories)

$items = array(
        array('id' => 42, 'parent_id' => 1),
        array('id' => 43, 'parent_id' => 42),
        array('id' => 1,  'parent_id' => 0),
);

$childs = array();
foreach($items as &$item) $childs[$item['parent_id']][] = &$item;
unset($item);

foreach($items as &$item) if (isset($childs[$item['id']]))
        $item['childs'] = $childs[$item['id']];
unset($item);

$tree = $childs[0];

http://stackoverflow.com/questions/4843945/php-tree-structure-for-categories-and-sub-categories-without-looping-a-query

Encoding conversion in terminal

-f == from

-t == to

iconv -f UTF-8 -t ISO-8859-15

(It only works w/ streams) ie:

cat ORIGIN | iconv -f UTF-8 -t ISO-8859-15 > DESTINATION

VarDumping to an error log

$x = "My string";
// Dump x
ob_start();
var_dump($x);
$contents = ob_get_contents();
ob_end_clean();
error_log($contents);

Mejor y más sencillo

$cadena = var_export($response,true);
error_log($cadena);

Dumping a Doctrine Entity

\Doctrine\Common\Util\Debug::dump($user);

Launch iPhone simulator from the term (mac)

open /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone\ Simulator.app

Enviar email con SwiftMailer en Symfony desde un comando de terminal

$transport = $this->container->get('mailer')->getTransport();
if (!$transport instanceof \Swift_Transport_SpoolTransport) {
    return;
}

$spool = $transport->getSpool();
if (!$spool instanceof \Swift_MemorySpool) {
    return;
}

//El quid de la cuestión!
$spool->flushQueue($this->container->get('swiftmailer.transport.real'));

(vía http://stackoverflow.com/a/13123285/1102136 )

Eliminar un Bundle de Symfony2

1. delete /src/Test/BlogBundle directory
2. change /app/config/routing.yml file to remove the bundle routes
3. remove your new bundle from /app/AppKernel.php
4. clear cache (either by deleting cache/{$env} or console cache:clear)

(vía http://stackoverflow.com/questions/12142425/symfony-2-how-to-delete-a-bundle )

Años entre dos fechas:

//$tz  = new DateTimeZone('Europe/Madrid');
        $age = DateTime::createFromFormat('d/m/Y', '13/12/2000')
            ->diff(new DateTime('now'))
            ->y;

(vía http://stackoverflow.com/a/3776832)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment