snipt

Ctrl+h for KB shortcuts

PHP

Social sharing links, properly sanitized

1
2
3
4
5
6
7
<?php
$share_title = get_the_title();
$share_link  = get_permalink();
?>
<a href="http://www.facebook.com/sharer.php?u=<?php echo urlencode($share_link); ?>" class="share-link facebook" target="_blank"><?php get_inline_svg('icon-social-facebook-f.svg'); ?><span class="screen-reader-text">Share on Facebook</span></a>
<a href="https://twitter.com/intent/tweet?url=<?php echo urlencode($share_link); ?>&amp;via=vital_design&amp;text=<?php echo esc_attr($share_title); ?>" class="share-link twitter" target="_blank"><?php get_inline_svg('icon-social-twitter.svg'); ?><span class="screen-reader-text">Share on Twitter</span></a>
<a href="mailto:?subject=<?php echo esc_attr($share_title); ?>&body=<?php echo esc_attr($share_title); ?>%0A<?php echo esc_url($share_link); ?>" class="share-link email"><?php get_inline_svg('icon-social-email.svg'); ?><span class="screen-reader-text">Share via Email</span></a>
https://snipt.net/embed/9b7efb032b0042ce48cd398c504f7ed8/
https://snipt.net/raw/9b7efb032b0042ce48cd398c504f7ed8/
9b7efb032b0042ce48cd398c504f7ed8
php
PHP
7
2016-09-24T09:26:12
True
False
False
Aug 12, 2016 at 04:05 PM
/api/public/snipt/147552/
social-sharing-links-properly-sanitized
<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></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="nv">$share_title</span> <span class="o">=</span> <span class="nx">get_the_title</span><span class="p">();</span> </span><span id="L-3"><a name="L-3"></a><span class="nv">$share_link</span> <span class="o">=</span> <span class="nx">get_permalink</span><span class="p">();</span> </span><span id="L-4"><a name="L-4"></a><span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-5"><a name="L-5"></a><span class="x">&lt;a href=&quot;http://www.facebook.com/sharer.php?u=</span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nb">urlencode</span><span class="p">(</span><span class="nv">$share_link</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x">&quot; class=&quot;share-link facebook&quot; target=&quot;_blank&quot;&gt;</span><span class="cp">&lt;?php</span> <span class="nx">get_inline_svg</span><span class="p">(</span><span class="s1">&#39;icon-social-facebook-f.svg&#39;</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x">&lt;span class=&quot;screen-reader-text&quot;&gt;Share on Facebook&lt;/span&gt;&lt;/a&gt;</span> </span><span id="L-6"><a name="L-6"></a><span class="x">&lt;a href=&quot;https://twitter.com/intent/tweet?url=</span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nb">urlencode</span><span class="p">(</span><span class="nv">$share_link</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x">&amp;amp;via=vital_design&amp;amp;text=</span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nx">esc_attr</span><span class="p">(</span><span class="nv">$share_title</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x">&quot; class=&quot;share-link twitter&quot; target=&quot;_blank&quot;&gt;</span><span class="cp">&lt;?php</span> <span class="nx">get_inline_svg</span><span class="p">(</span><span class="s1">&#39;icon-social-twitter.svg&#39;</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x">&lt;span class=&quot;screen-reader-text&quot;&gt;Share on Twitter&lt;/span&gt;&lt;/a&gt;</span> </span><span id="L-7"><a name="L-7"></a><span class="x">&lt;a href=&quot;mailto:?subject=</span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nx">esc_attr</span><span class="p">(</span><span class="nv">$share_title</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x">&amp;body=</span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nx">esc_attr</span><span class="p">(</span><span class="nv">$share_title</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x">%0A</span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nx">esc_url</span><span class="p">(</span><span class="nv">$share_link</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x">&quot; class=&quot;share-link email&quot;&gt;</span><span class="cp">&lt;?php</span> <span class="nx">get_inline_svg</span><span class="p">(</span><span class="s1">&#39;icon-social-email.svg&#39;</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x">&lt;span class=&quot;screen-reader-text&quot;&gt;Share via Email&lt;/span&gt;&lt;/a&gt;</span> </span></pre></div> </td></tr></table>
sanitize, security, share, social, validate

JavaScript

JavaScript Validate Email

1
2
3
4
function IsEmail(email) {
  var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}
https://snipt.net/embed/7a056c59c290959107741aae4a2e804a/
https://snipt.net/raw/7a056c59c290959107741aae4a2e804a/
7a056c59c290959107741aae4a2e804a
js
JavaScript
4
2016-09-22T02:29:24
True
False
False
Mar 07, 2013 at 10:37 AM
/api/public/snipt/57216/
javascript-validate-email
<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 id="L-1"><a name="L-1"></a><span class="kd">function</span> <span class="nx">IsEmail</span><span class="p">(</span><span class="nx">email</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="kd">var</span> <span class="nx">regex</span> <span class="o">=</span> <span class="sr">/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a> <span class="k">return</span> <span class="nx">regex</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">email</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
email, validate

MySQL

Select users where email is invalid

1
2
3
SELECT * 
FROM users
WHERE email NOT REGEXP  '^[A-Z0-9\._%-]+@[A-Z0-9\.-]+\.[A-Z]{2,4}$'
https://snipt.net/embed/712560d14b48df6885ae6618f92e8397/
https://snipt.net/raw/712560d14b48df6885ae6618f92e8397/
712560d14b48df6885ae6618f92e8397
mysql
MySQL
3
2016-09-22T03:17:11
True
False
False
Oct 09, 2012 at 11:11 AM
/api/public/snipt/48307/
select-users-where-email-is-invalid
<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 id="L-1"><a name="L-1"></a><span class="k">SELECT</span> <span class="o">*</span> </span><span id="L-2"><a name="L-2"></a><span class="k">FROM</span> <span class="n">users</span> </span><span id="L-3"><a name="L-3"></a><span class="k">WHERE</span> <span class="n">email</span> <span class="k">NOT</span> <span class="k">REGEXP</span> <span class="s1">&#39;^[A-Z0-9\._%-]+@[A-Z0-9\.-]+\.[A-Z]{2,4}$&#39;</span> </span></pre></div> </td></tr></table>
email, mysql, regex, validate

PHP

Validate domain name

1
2
3
4
5
6
$url = "https://snipt.net/";
if (preg_match('/^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?/i', $url)) {
    echo "Your url is ok.";
} else {
    echo "Wrong url.";
}
https://snipt.net/embed/f5ed73e75cd63fe737818de7c6559938/
https://snipt.net/raw/f5ed73e75cd63fe737818de7c6559938/
f5ed73e75cd63fe737818de7c6559938
php
PHP
6
2016-09-26T06:49:43
True
False
False
/api/public/snipt/44645/
validate-domain-name
<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 id="L-1"><a name="L-1"></a><span class="x">$url = &quot;https://snipt.net/&quot;;</span> </span><span id="L-2"><a name="L-2"></a><span class="x">if (preg_match(&#39;/^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?/i&#39;, $url)) {</span> </span><span id="L-3"><a name="L-3"></a><span class="x"> echo &quot;Your url is ok.&quot;;</span> </span><span id="L-4"><a name="L-4"></a><span class="x">} else {</span> </span><span id="L-5"><a name="L-5"></a><span class="x"> echo &quot;Wrong url.&quot;;</span> </span><span id="L-6"><a name="L-6"></a><span class="x">}</span> </span></pre></div> </td></tr></table>
domain, validate

JavaScript

Validate email

1
2
3
4
5
6
function _validateEmail( $email )
{
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	return re.test( $email );
}
	
https://snipt.net/embed/6e94846f1e6ed2e2154d3149a03c588a/
https://snipt.net/raw/6e94846f1e6ed2e2154d3149a03c588a/
6e94846f1e6ed2e2154d3149a03c588a
js
JavaScript
6
2016-09-23T17:41:17
True
False
False
Jul 07, 2012 at 07:08 PM
/api/public/snipt/43587/
validate-email-7
<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 id="L-1"><a name="L-1"></a><span class="kd">function</span> <span class="nx">_validateEmail</span><span class="p">(</span> <span class="nx">$email</span> <span class="p">)</span> </span><span id="L-2"><a name="L-2"></a><span class="p">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="kd">var</span> <span class="nx">re</span> <span class="o">=</span> <span class="sr">/^(([^&lt;&gt;()[\]\\.,;:\s@\&quot;]+(\.[^&lt;&gt;()[\]\\.,;:\s@\&quot;]+)*)|(\&quot;.+\&quot;))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">return</span> <span class="nx">re</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span> <span class="nx">$email</span> <span class="p">);</span> </span><span id="L-5"><a name="L-5"></a><span class="p">}</span> </span><span id="L-6"><a name="L-6"></a> </span></pre></div> </td></tr></table>
email, validate, validation
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.