snipt

Ctrl+h for KB shortcuts

PHP

regex - replace invalid chars with underscore

1
2
3
<?
// replace invalid chars with underscore (good for filename)
$name = preg_replace("/[^a-z0-9-]/", "_" ,$name);
https://snipt.net/embed/535ae03d189dc9c9281698e525f5dd35/
/raw/535ae03d189dc9c9281698e525f5dd35/
535ae03d189dc9c9281698e525f5dd35
php
PHP
3
2019-07-19T10:37:47
True
False
False
/api/public/snipt/8204/
regex-replace-invalid-chars-with-underscore
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">// replace invalid chars with underscore (good for filename)</span> </span><span id="L-3"><a name="L-3"></a><span class="nv">$name</span> <span class="o">=</span> <span class="nb">preg_replace</span><span class="p">(</span><span class="s2">&quot;/[^a-z0-9-]/&quot;</span><span class="p">,</span> <span class="s2">&quot;_&quot;</span> <span class="p">,</span><span class="nv">$name</span><span class="p">);</span> </span></pre></div> </td></tr></table>
php, regex

JavaScript

jquery - check if element exist

1
2
3
4
5
6
// ===================
// jquery - check if element or tag exist

if ($('#myDiv').length) {
    // your code
}
https://snipt.net/embed/78f587d0bd0509b4d98aa6790a4b8ca4/
/raw/78f587d0bd0509b4d98aa6790a4b8ca4/
78f587d0bd0509b4d98aa6790a4b8ca4
js
JavaScript
6
2019-07-15T09:26:20
True
False
False
/api/public/snipt/8203/
jquery-check-if-element-exist
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">// ===================</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">// jquery - check if element or tag exist</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="k">if</span> <span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#myDiv&#39;</span><span class="p">).</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-5"><a name="L-5"></a> <span class="c1">// your code</span> </span><span id="L-6"><a name="L-6"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
jquery

JavaScript

jquery zebra table striping

1
2
3
4
// ===================
// jquery zebra table odd striping

$("tr:nth-child(odd)").addClass("odd");
https://snipt.net/embed/ca663f768a7e8e6ddcb23f5b5cc44935/
/raw/ca663f768a7e8e6ddcb23f5b5cc44935/
ca663f768a7e8e6ddcb23f5b5cc44935
js
JavaScript
4
2019-07-19T09:31:09
True
False
False
/api/public/snipt/8202/
jquery-zebra-table-striping
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">// ===================</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">// jquery zebra table odd striping</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;tr:nth-child(odd)&quot;</span><span class="p">).</span><span class="nx">addClass</span><span class="p">(</span><span class="s2">&quot;odd&quot;</span><span class="p">);</span> </span></pre></div> </td></tr></table>
jquery, striping, tables, zebra

JavaScript

Simple example of how to write your own jquery

1
2
3
4
5
6
7
8
// ===================
// writing your own jquery function

$.fn.makeRed = function() {  
     return $(this).css('background', 'red');  
}  
   
$('#myTable').find('.firstColumn').makeRed();
https://snipt.net/embed/5a836609ec47ceb0a45b4eac1768c3e3/
/raw/5a836609ec47ceb0a45b4eac1768c3e3/
5a836609ec47ceb0a45b4eac1768c3e3
js
JavaScript
8
2019-07-14T03:54:30
True
False
False
/api/public/snipt/8201/
simple-example-of-how-to-write-your-own-jquery
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">// ===================</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">// writing your own jquery function</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="nx">$</span><span class="p">.</span><span class="nx">fn</span><span class="p">.</span><span class="nx">makeRed</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span> </span><span id="L-5"><a name="L-5"></a> <span class="k">return</span> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">css</span><span class="p">(</span><span class="s1">&#39;background&#39;</span><span class="p">,</span> <span class="s1">&#39;red&#39;</span><span class="p">);</span> </span><span id="L-6"><a name="L-6"></a><span class="p">}</span> </span><span id="L-7"><a name="L-7"></a> </span><span id="L-8"><a name="L-8"></a><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#myTable&#39;</span><span class="p">).</span><span class="nx">find</span><span class="p">(</span><span class="s1">&#39;.firstColumn&#39;</span><span class="p">).</span><span class="nx">makeRed</span><span class="p">();</span> </span></pre></div> </td></tr></table>
javascript, jquery