Browse ALL the snippets ! - Page 4 of 6

Get more space for your text in gVim, add it to your .vimrc !

set guioptions-=m  "remove menu bar
set guioptions-=T  "remove toolbar
set guioptions-=r  "remove right-hand scroll bar
set guioptions-=M

Useful for Symfony2 or Silex as they load an instance of the app in Twig, this snippet allows to check if you're currently at its root, no matter it's in a subdirectory on your web server or not.

{% if app.request.getRequestURI()|rtrim('/') == app.request.getBasePath() %}
    {# do something #}
{% endif %}

Use this to get a summary for a blog article, for example : it returns a tag-stripped substring, cut at the end of the last entire word, appended with ellipses if necessary.

function getSummary($text, $length = 150)
{
    $text = trim(preg_replace('~(\s+)~', ' ', strip_tags($text)));
    if (strlen($text) > $length)
    {
        $cut = substr($text, 0, $length);
        if (substr($text, $length, 1) != ' ')
            $cut = substr($cut, 0, strrpos($cut, ' '));
        $text = $cut.'...';
    }
    return $text;
}

Just a quick tip to extend jQuery so you can set or unset a class to an element (or group of elements) depending on a boolean condition :

$.fn.setClass = function(myclass, action) {
    this.hasClass(myclass) == !! action || this.toggleClass(myclass);
    return this;
};

// Then do
$('#myelement').setClass('americaine', myBooleanFunction());

This snippet includes two functions : one that retrieves an array's longest row length, and another that transposes it according to the title (and depends on the first). It can come handy when needing to access simple data the other way around.

function array_longer_row($array)
{
    $max = 0;
    $lens = array();
    foreach ($array as $row)
    {
        $len = 1;
        if (is_array($row))
            $len = count($row);
        $lens[] = $len;
    }
    return max($lens);
}



function array_transpose($array)
{
    $result = array();
    $max = array_longer_row($array);
    for ($i=0;$i<$max;$i++)
    {
        $result[$i] = array();
        foreach ($array as $key => $row)
        {
            if (is_array($row))
                $row = array_values($row);
            else
                $row = array($row);
            if (isset($row[$i]))
                $result[$i][] = $row[$i];
        }
    }
    return $result;
}

Here's a quick function that may be useful if you have to compare a bunch of value in a row. You can pass your parameters to it directly, as an array or as a JS object. It depends on jQuery for array/object conversion as IE < 9 doesn't know Array.map().

function anymax()
{
    var argv = anymax.arguments, argc = argv.length, max = false;

    if (argc)
    {
        max = argv[0];

        if (typeof max === 'object')
        {
            argv = jQuery.map(max, function(v, k) { return v; });
            max = argv[0];
        }

        for (i in argv)
            max = Math.max(max, argv[i]);
    }

    return max;
}

Saves you quite a bunch of time ! Run xmodmap on a file with the following content.

remove Lock = Caps_Lock                                                 
keysym Caps_Lock = Escape

Update DynDns with CURL (php)

function doCurl($strDynURL){
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$strDynURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_HTTPHEADER,array('User-Agent: Billion - VoIP/802.11g - 1.1'));
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

return yes or no if string found

SELECT IF(LENGTH(SUBSTRING('yo needle', instr('yo needle', 'needle'), LENGTH('yo needle')))>0,1,0) AS result

Avoid class researches or precise them.

//You should not use 
$(".bob").hide();

//but use instead
$("#closestParent .bob, #someOtherPlaceNearBob .bob").hide();