SnippetVamp

Parce que yen a marre de chercher ses bouts de code partout...



TODO 1 ajax 3 astuce 66 auto_ 2 bash 1 bootstrap 14 conversion 2 crossbrowser 3 css 48 debug 10 dothtaccess 1 effets 1 erreurs 1 evenements 2 fallback 1 fichiers 27 filter 1 fonctions 73 form 1 graphisme 11 html 39 javascript 39 jquery 26 lib 2 mail 1 markdown 2 mobile 2 parser 13 php 88 pluXML 4 python 10 responsive 8 securite 16 social 1 texte 16 vanilla 4

.

Derniers snippets

Bienvenue sur l'espace SnippetVamp de Bronco.
SnippetVamp est une application libre et open-source destinée à conserver, créer, modifier, retrouver, gérer, partager simplement et rapidement des snippets.
Vous pouvez la télécharger sur Github (github.com/broncowdd/SnippetVamp), l'installer sur votre serveur (php 5.2 sans base de données) et en modifier les thèmes et le code comme bon vous semble (en laissant quelque part une référence à la source ;) )

Strip_cars

function strip_tags(input, allowed) {
                //http://phpjs.org/functions/strip_tags/
                allowed = (((allowed || '') + '')
                .toLowerCase()
                .match(/<[a-z][a-z0-9]*>/g) || [])
                .join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
                var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
                commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
                return input.replace(commentsAndPhpTags, '')
                .replace(tags, function($0, $1) {
                  return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
                });
            }

astuce fonctions javascript securite

http://phpjs.org/functions/strip_tags/

<iframe width="100%" height="416" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=53c57f665cb65" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 15/07/2014

strip_almost_all_tags

function strip_almost_all_tags($chaine){
    // ajouter le strip attributes
    // à la barbare
    $secure='#on(click|dblclick|blur|change|dragdrop|focus|keydown|keyup|keypress|mouseover|mouseout|load|unload|keydown)=#i';
    $keep=array('<img ','<a ', '<em', '<br>','<br/>','</a', '</em', '<br />','<p ', '</p','<div ', '</div','<span ', '</span', '<li', '</li', '<ul', '</ul');
    $temp_repl=array('[img ','[a ','[em','[br]','[br/]','[/a','[/em','[br /]','[p ','[/p','[div ','[/div','[span ','[/span','[li','[/li','[ul','[/ul');
    $chaine=str_ireplace($keep,$temp_repl,$chaine);
    $chaine=strip_tags($chaine);
    $chaine=preg_replace($secure, 'on$1&#61;', $chaine);
    return str_ireplace($temp_repl,$keep,$chaine);
}

fonctions php securite texte

<iframe width="100%" height="380" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=53c5565a631c5" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 23/09/2014

Raccourci clavier textarea pour valider [Résolu]

<script type="text/javascript">
window.onload = function() {

    /* <textarea id="comment_textarea" ...></textarea> */
    var textarea = document.getElementById('comment_textarea');
    
    textarea.onkeydown = function(e) {
        e = e || event; // cross-browser (quand IE en fait � sa t�te)
        
        /* Envoyer le formulaire avec Ctrl+Entrer */
        if(e.keyCode == 13 && e.ctrlKey) {
            this.form.submit();
        }
        
        /* Envoyer le formulaire avec Entrer mais autoriser
         * les retours � la ligne avec Ctrl+Entrer */
        if(e.keyCode == 13 && !e.ctrlKey) {
            this.form.submit();
            return false;
        } else if (e.keyCode == 13) {
            this.value += "\n";
        }
    };
};
</script>


evenements javascript

http://www.commentcamarche.net/forum/affich-22760593-raccourci-clavier-textarea-pour-valider

<iframe width="100%" height="668" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=53c3c4331bcf3" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 14/07/2014

poster des données en asynchrone sans jquery

        function post(){
            obj=document.getElementById('message');
            data=obj.value;
            request = new XMLHttpRequest;
            request.open('POST', 'index.php', true);
            
            request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            request.send("message="+data+"&pseudo=roger");// ici on met name=valeur
            obj.value='';
        }

ajax javascript

<iframe width="100%" height="362" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=53c0f444c22f3" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 12/07/2014

only_alphanum_and_dot - filtrer les caractères pour un fichier

function only_alphanum_and_dot($string){return preg_replace('#[^a-zA-Z0-9\. ]#','',$string);}

fichiers fonctions php

<iframe width="100%" height="218" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=53b15e68b8334" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 30/06/2014

inclure un fichier contenu dans le dossier du thème

<?php include(PLX_ROOT.$plxMotor->aConf['racine_themes'].$plxMotor->style.'/php/monfichier.php') ?>

astuce pluXML

http://pluxopolis.net/article41/inclure-un-fichier-php-stocke-dans-le-dossier-de-son-theme

<iframe width="100%" height="200" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=53a28f77f0747" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 19/06/2014

inclure une page statique (dans la sidebar ou ailleurs)

<?php $plxShow->staticInclude('002') ?>

astuce pluXML

http://pluxopolis.net/article16/afficher-le-contenu-d-une-page-statique-dans-la-sidebar

<iframe width="100%" height="200" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=53a28f28991fb" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 19/06/2014

un fallback à svg (utilisable ailleurs)

<img src="logo.svg" onerror="this.src = 'logo.png';" alt="logo" width="140" height="140"/>

astuce css javascript

https://tiger-222.fr/shaarli/?svc7Bw

<iframe width="100%" height="200" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=53a040c71f669" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 17/06/2014

Générer un breadcrumb à partir d'une URL

<?php 
// this function generates a simple breadcrumb 
if ( ! function_exists('breadcrumb')) { 
    function breadcrumb($data = array()) 
    { 
        $breadcrumb = '<ul class="breadcrumb">'."\n"; 
        if(isset($data)) { 
            foreach ($data as $link) { 
                if((isset($link[2])) AND ($link[2] === TRUE)) { 
                    $breadcrumb .= '<li class="active"><span>'.$link[1].'</span></li>'."\n"; 
                } else { 
                    $breadcrumb .= '<li><a href="'.$link[0].'">'.$link[1].'</a></li>'."\n"; 
                } 
            } 
        } 
        $breadcrumb .= '</ul>'."\n"; 

        return $breadcrumb; 
    } 
} 

// Below is an example of how to generate the breadcrumb 
$breadcrumb = array( 
    array('http://localhost/', 'Home'), 
    array('', 'Register', TRUE), 
    ); 

echo breadcrumb($breadcrumb); 

/* 
|---------------------------------------------------------------- 
| This will echo 
| 
| <ul class="breadcrumb"> 
|   <li><a href="http://localhost/">Home</a></li> 
|   <li class="active"><span>Register</span></li> 
| </ul> 
|---------------------------------------------------------------- 
| 
| You can use the following css 
| [credit to : iTemplates | http://dinakit.itemplat.es/] 
| 
| .breadcrumb { background-color: #FEFEFE; list-style: none; margin: 0 0 20px; padding: 8px 15px; font-size: 12px; } 
| .breadcrumb > li { display: inline-block; *display: inline; *zoom: 1; } 
| .breadcrumb > li:after { content: "\2022"; color: #DDDDDD; padding: 0 5px 0 10px; } 
| .breadcrumb > li:last-child:after { content: none; } 
| .breadcrumb > li > a:link, 
| .breadcrumb > li > a:visited { text-decoration: none; } 
| .breadcrumb > li > a:hover, 
| .breadcrumb > li > a:active, 
| .breadcrumb > li > a:focus { text-decoration: underline; } 
| .breadcrumb > .active > span, 
| .breadcrumb > .active > a { color: #999999; text-decoration: none; cursor: default; } 
*/

fonctions php

http://phpsnips.com/608/Generate-Breadcrumbs

<iframe width="100%" height="1154" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=539de3b6038dc" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 15/06/2014

get_video_infos - infos sur une video youtube

function file_curl_contents($url,$pretend=true){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Charset: UTF-8'));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,  FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        if (!ini_get("safe_mode") && !ini_get('open_basedir') ) {curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);}
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
        if ($pretend){curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/29.0');}
    
        curl_setopt($ch, CURLOPT_REFERER, '');
        $data = curl_exec($ch);
        $response_headers = curl_getinfo($ch);

        // Google seems to be sending ISO encoded page + htmlentities, why??
        if($response_headers['content_type'] == 'text/html; charset=ISO-8859-1') $data = html_entity_decode(iconv('ISO-8859-1', 'UTF-8//TRANSLIT', $data)); 
        
        # $data = curl_exec($ch);

        curl_close($ch);

        return $data;
    }


function get_video_infos($url=null){
    // returns an array with all the youtube video's informations (available qualities/urls/format...)
    if (!empty($url)){
        if ($contenu=file_curl_contents($url)){
            preg_match_all('#"url_encoded_fmt_stream_map": "(.*?)"#', $contenu, $resultats);
            preg_match_all('#<title>(.*?)</title>#', $contenu, $title);
            $c=0;
            $p=array('title'=>$title[1][0]);
            $params=explode('\u0026',$resultats[1][0]); 
            foreach ($params as $param){
                $temp=explode('=',$param);
                if (isset($p[$c][$temp[0]])){$c++;}
                $p[$c][$temp[0]]=urldecode($temp[1]);
            }
            return $p;
        }else{return false;}
    }else{return false;}

}


echo '<pre>';print_r(get_video_infos('http://www.youtube.com/watch?v=oHg5SJYRHA0'));

fonctions parser php

http://warriordudimanche.net

<iframe width="100%" height="1046" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=538dc80ed0d39" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 05/06/2014

ScrollToTop sans jQuery - Ecyseo

/*
# ------------------ BEGIN LICENSE BLOCK ------------------
#
#
# Copyright (c) 2009 - 2014 Cyril MAGUIRE
# Licensed under the CeCILL v2.1 license.
# See http://www.cecill.info/licences.fr.html
#
# ------------------- END LICENSE BLOCK -------------------
*/
;(function(window,undefined) {

    'use_strict';

    var timeOut;
    var isIE = isIE();

    function isIE() {
        var nav = navigator.userAgent.toLowerCase();
        return (nav.indexOf('msie') != -1) ? parseInt(nav.split('msie')[1]) : false;
    }

    function backToTop() {
        if (document.body.scrollTop!=0 || document.documentElement.scrollTop!=0){
            window.scrollBy(0,-50);
            timeOut=setTimeout('backToTop()',40);
        }
        else {
            clearTimeout(timeOut);
        }
    }

    function getScrollPosition() {
        return Array((document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft,(document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop);
    }

    function Remove(idOfParent,idToRemove) {
        if (isIE) {
            document.getElementById(idToRemove).removeNode(true);
        } else {
            var Node1 = document.getElementById(idOfParent); 
            var len = Node1.childNodes.length;
            
            for(var i = 0; i < len; i++){           
                if (Node1.childNodes[i] != undefined && Node1.childNodes[i].id != undefined && Node1.childNodes[i].id == idToRemove){
                    Node1.removeChild(Node1.childNodes[i]);
                }
            }
        }   
    }

    function addElement(idOfParent,idToAdd,htmlToInsert) {
        var DomParent = document.getElementById(idOfParent);//id of body
        var newdiv = document.createElement('div');

        newdiv.setAttribute('id',idToAdd);
        newdiv.innerHTML = htmlToInsert;

        DomParent.appendChild(newdiv);
    }

    function displayBackButton() {
        var pos = [];
        var fleche = '\u21E7';

        if (isIE) {
            fleche = '\u25B2';
        }
        pos = getScrollPosition();
        var topLink = document.getElementById('toplink');
        if (pos[1] > 150) {
            if (topLink == null) {
                addElement('top','toplink',''+fleche+'');
            }
        } else {
            if (topLink != null) {
                Remove('top','toplink');
            }
        }
    }

    //add to global namespace
    window.onscroll = displayBackButton;
    window.displayBackButton = displayBackButton;
    window.backToTop = backToTop;


})(window);  
    

// CSS
#toplink {
    position: fixed;
    bottom: 20px;
    width: 100px;
    text-align: center;
    right:10px;
}
#toplink a { 
    font-size: 40px;
    opacity: 0.8;
}


fonctions javascript

http://www.ecyseo.net/article35/scrolltotop-sans-jquery-en-pur-javascript

<iframe width="100%" height="2054" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=5386cc4bd8af4" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 29/05/2014

Connaitre la position du scroll dans la page


function getScrollPosition()
{
    return Array((document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft,(document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop);
}

fonctions html javascript

http://dev.petitchevalroux.net/javascript/connaitre-position-scroll-dans-page-javascript.89.html

<iframe width="100%" height="272" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=53861e0624680" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 28/05/2014

Injecter javascript en asynchrone externe (non bloquant)

<!-- BAD: blocking external script -->
<script src="http://somehost.com/awesome-widget.js"></script>

<!-- GOOD: remote script is loaded asynchronously -->
<script>
    var script = document.createElement('script');
    script.src = "http://somehost.com/awesome-widget.js";
    document.getElementsByTagName('head')[0].appendChild(script);
</script>

<!-- GOOD: modern, simpler, faster, and better all around  -->
<script src="http://somehost.com/awesome-widget.js" async></script>

astuce javascript

https://www.igvita.com/2014/05/20/script-injected-async-scripts-considered-harmful/

<iframe width="100%" height="398" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=5382d54e27643" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 26/05/2014

file_curl_contents + referrer custom

    function file_curl_contents($url,$pretend=true){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Charset: UTF-8'));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,  FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        if (!ini_get("safe_mode") && !ini_get('open_basedir') ) {curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);}
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
        if ($pretend){curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0');}
    
        curl_setopt($ch, CURLOPT_REFERER, random_referer());// notez le referer "custom"

        $data = curl_exec($ch);
        $response_headers = curl_getinfo($ch);

        // Google seems to be sending ISO encoded page + htmlentities, why??
        if($response_headers['content_type'] == 'text/html; charset=ISO-8859-1') $data = html_entity_decode(iconv('ISO-8859-1', 'UTF-8//TRANSLIT', $data)); 
        
        # $data = curl_exec($ch);

        curl_close($ch);

        return $data;
    }


    function Random_referer(){
        return array_rand(array(
            'http://oudanstoncul.com.free.fr/‎',
            'http://googlearretedenousfliquer.fr/‎',
            'http://stopspyingme.fr/‎',
            'http://spyyourassfuckinggoogle.fr/‎',
            'http://dontfuckinglookatme.fr/‎',
            'http://matemonculgoogle.fr/‎',
            'http://auxarmescitoyens.fr/‎',
            'http://jetlametsavecdugravier.con/‎',
            'http://lesdeuxpiedsdanstagueule.fr/‎',
            'http://moncoudedanstabouche.con/‎',
            'http://monpieddanston.uk/‎',
            'http://bienfaitpourvosgueul.es/‎',
            'http://pandanstesdents.fr/‎',
            'http://tupuessouslesbras.fr/‎',
            'http://mangetescrottesdenez.fr/‎',
            'http://jtepourristesstats.fr/‎',
            'http://ontecompissevigoureusement.com/‎',
            'http://lepoingleveetlemajeuraussi.com/‎',
        ));
    }

fichiers fonctions php

<iframe width="100%" height="1064" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=535a798f95b35" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 25/04/2014

Demander confirmation de la fermeture d'un onglet

var confirmOnLeave = function(msg) {
 
    window.onbeforeunload = function (e) {
        e = e || window.event;
        msg = msg || '';
 
        // For IE and Firefox
        if (e) {e.returnValue = msg;}
 
        // For Chrome and Safari
        return msg;
    };
 
};
 
// message de confirmation générique du navigateur
confirmOnLeave();
 
// message de confirmation personnalisé
confirmOnLeave('Vous allez perdre votre travail, êtes vous sûr(e) de vouloir quitter la page ?');

fonctions javascript

http://sametmax.com/demander-confirmation-a-la-fermeture-dun-onglet-en-javascript/

<iframe width="100%" height="542" src="http://snippetvamp.warriordudimanche.net/snippetvamp.php?embed=534d0559417e3" type="text/html"></iframe>

Texte seul - Permalink - Snippet public posté le 15/04/2014

123456789101112131415

Flux RSS de cette page


SnippetVamp 1.83 par Bronco - Page générée en 0.012 s