Skip to content

Instantly share code, notes, and snippets.

View melat0nin's full-sized avatar
✍️

melat0nin

✍️
  • Edinburgh, Scotland
View GitHub Profile
@melat0nin
melat0nin / gist:1751745
Created February 6, 2012 11:55
PHP: Minify CSS
// from here: http://www.concrete5.org/documentation/how-tos/developers/dynamically-concatenate-minify-gzip-and-server-side-cache-multip/
function minifyCSS($css) {
$css = trim($css);
$css = str_replace("\r\n", "\n", $css);
$search = array("/\/\*[^!][\d\D]*?\*\/|\t+/", "/\s+/", "/\}\s+/");
$replace = array(null, " ", "}\n");
$css = preg_replace($search, $replace, $css);
$search = array("/;[\s+]/", "/[\s+];/", "/\s+\{\\s+/", "/\\:\s+\\#/", "/,\s+/i", "/\\:\s+\\\'/i", "/\\:\s+([0-9]+|[A-F]+)/i", "/\{\\s+/", "/;}/");
$replace = array(";", ";", "{", ":#", ",", ":\'", ":$1", "{", "}");
@melat0nin
melat0nin / gist:1751735
Created February 6, 2012 11:53
c5: use page ID to get link
function getTheURL($url_id) {
$opg = Page::getById($url_id);
$url=Loader::helper('navigation');
$canonical=$url->getCollectionURL($opg);
$canonical=preg_replace("/index.php\?cID=1$/","",$canonical);
echo $canonical;
}
<a href="<?php getTheURL(140); ?>">
@melat0nin
melat0nin / gist:1751731
Created February 6, 2012 11:52
c5: get page's owner
$owner = $c->getVersionObject()->getVersionAuthorUserName();
@melat0nin
melat0nin / gist:1751730
Created February 6, 2012 11:52
PHP: Truncate text to x chars
function truncate($string,$words) {
$truncated = preg_replace('/((\w+\W*){' . $words . '}(\w+))(.*)/', '${1}', $string);
if ( strlen($string) > strlen($truncated) ) {
$truncated .= '...';
}
return $truncated;
}
@melat0nin
melat0nin / gist:1751726
Created February 6, 2012 11:51
c5: get current page's type
$c->getCollectionTypeHandle();
@melat0nin
melat0nin / gist:1751723
Created February 6, 2012 11:51
c5: if collection ID == x, do this
$currentPage = Page::getCurrentPage();
if ($currentPage->getCollectionID() == 1) {
...
}
@melat0nin
melat0nin / gist:1751722
Created February 6, 2012 11:51
c5: create Area
$a = new Area('Main');
$a->display($c);
@melat0nin
melat0nin / gist:1751719
Created February 6, 2012 11:50
c5: hardcode stack/block
// Stack
$nav = Stack::getByName('main_nav');
$nav->display();
// Block
$nav = Block::getByName('main_nav');
$nav->display();
@melat0nin
melat0nin / gist:1751714
Created February 6, 2012 11:49
c5: check if user is logged in
$u = new User;
if ( $u->isLoggedIn() ){
...
}
@melat0nin
melat0nin / gist:1751711
Created February 6, 2012 11:49
c5: If in edit mode
global $c;
if ($c->isEditMode()) {
}