Skip to content

Instantly share code, notes, and snippets.

View fabiocicerchia's full-sized avatar
🏠
Working from home

Fabio Cicerchia fabiocicerchia

🏠
Working from home
View GitHub Profile
<?php
function safe_truncate_text($text, $lenght)
{
$trailingDots = '';
$truncatedText = substr($text . ' ', 0, $lenght);
if (strlen($text) > $lenght)
{
$truncatedText = preg_replace('/ [^ ]+$/', '', ' ' . $truncatedText);
@fabiocicerchia
fabiocicerchia / SampleObject.class.php
Created February 24, 2012 16:26
Doctrine 1.x - Force an ID
<?php
class SampleObject extends Doctrine_Record
{
public function setId($value)
{
$this->_values['id'] = $value;
$this->_set('id', $value);
}
}
@fabiocicerchia
fabiocicerchia / namespace.js
Created February 25, 2012 08:24
Javascript - Register a namespace
function registerNS(ns) {
var nsParts = ns.split('.');
var root = window;
for(var i = 0; i < nsParts.length; i++) {
if(typeof root[nsParts[i]] == 'undefined') {
root[nsParts[i]] = new Object();
}
root = root[nsParts[i]];
}
@fabiocicerchia
fabiocicerchia / is_ajax.php
Created February 25, 2012 08:27
PHP - Check if a request is really AJAX
<?php
function is_ajax() {
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
}
@fabiocicerchia
fabiocicerchia / unhtmlentities.php
Last active December 1, 2020 22:08
PHP - Remove HTML entities
<?php
function unhtmlentities($string) {
$string = preg_replace_callback('~&#x([0-9a-f]+);~i', function ($matches) { return chr(hexdec($matches[0])); }, $string);
$string = preg_replace_callback('~&#([0-9]+);~', function ($matches) { return chr((int) $matches[0]); }, $string);
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
$trans_tbl = array_flip($trans_tbl);
return strtr($string, $trans_tbl);
@fabiocicerchia
fabiocicerchia / gist:1907341
Created February 25, 2012 08:33
SQLite - Migrate DB from v2 to v3 via shell
sqlite old.db .dump | sqlite3 new.db
@fabiocicerchia
fabiocicerchia / gist:3089145
Created July 11, 2012 08:57
MongoDB - Increase the number of results
DBQuery.shellBatchSize = 300
@fabiocicerchia
fabiocicerchia / .bashrc
Created July 23, 2012 08:43
.bashrc PS1 for SVN & GIT
PS1='${debian_chroot:+($debian_chroot)}[\[\033[0;37m\]\t\[\033[00m\]] \[\033[0;94m\]\u\[\033[00m\] is `if [ \$? = 0 ]; then echo -e "\[\033[32m\]happy\[\033[00m\]"; else echo -e "\[\033[31m\]sad\[\033[00m\]"; fi` in \[\033[0;36m\]\w\[\033[00m\]$(
s=""
if [[ -d ".svn" ]] ; then
r=`svn info 2>&1 | grep "Revision: " | cut -d " " -f 2`
b=
url=`svn info 2>&1 | awk "/URL:/ {print $2}"`
if [[ $url =~ trunk ]]; then
b="trunk"
elif [[ $url =~ /branches/ ]]; then
@fabiocicerchia
fabiocicerchia / gist:3524994
Created August 30, 2012 09:46
PHP - Fetch all the mysqli results (one line of code)
for ($resultSet = array(); ($row = $result->fetch_object()) !== null; array_push($resultSet, $row));
@fabiocicerchia
fabiocicerchia / gist:3560894
Created August 31, 2012 23:19
MongoDB - Drop database using command line
mongo <DB> --eval 'db.dropDatabase();'