snipt

Ctrl+h for KB shortcuts

PHP

Regular expression in PHP to convert URLs to HTML links

1
2
3
4
5
6
7
<?php

$pattern = "@\b(https?://)?(([0-9a-zA-Z_!~*'().&=+$%-]+:)?[0-9a-zA-Z_!~*'().&=+$%-]+\@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((/[0-9a-zA-Z_!~*'().;?:\@&=+$,%#-]+)*/?)@";

$text = preg_replace($pattern, '<a target="_blank" rel="nofollow" href="\0">\0</a>', $text);

return $text;
https://snippets.siftie.com/embed/bb30df9731add63deb92ddbeb52b4153/
/raw/bb30df9731add63deb92ddbeb52b4153/
bb30df9731add63deb92ddbeb52b4153
php
PHP
7
2019-03-16T20:02:09
True
False
False
/api/public/snipt/16123/
regular-expression-in-php-to-convert-urls-to-html-links
<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></span><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><span id="L-3"><a name="L-3"></a><span class="nv">$pattern</span> <span class="o">=</span> <span class="s2">&quot;@\b(https?://)?(([0-9a-zA-Z_!~*&#39;().&amp;=+$%-]+:)?[0-9a-zA-Z_!~*&#39;().&amp;=+$%-]+\@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*&#39;()-]+\.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((/[0-9a-zA-Z_!~*&#39;().;?:\@&amp;=+$,%#-]+)*/?)@&quot;</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="nv">$text</span> <span class="o">=</span> <span class="nb">preg_replace</span><span class="p">(</span><span class="nv">$pattern</span><span class="p">,</span> <span class="s1">&#39;&lt;a target=&quot;_blank&quot; rel=&quot;nofollow&quot; href=&quot;\0&quot;&gt;\0&lt;/a&gt;&#39;</span><span class="p">,</span> <span class="nv">$text</span><span class="p">);</span> </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a><span class="k">return</span> <span class="nv">$text</span><span class="p">;</span> </span></pre></div> </td></tr></table>
links, php, regex, urls

Python

Reverse a named URL from view

1
2
3
from django.core.urlresolvers import reverse
return HttpResponseRedirect(reverse('todo-incomplete_tasks', \
    args=[task.list.id, task.list.slug]))
https://snippets.siftie.com/embed/e1ae68c07c00dad3063166bd90179cff/
/raw/e1ae68c07c00dad3063166bd90179cff/
e1ae68c07c00dad3063166bd90179cff
python
Python
3
2019-02-22T06:43:28
True
False
False
/api/public/snipt/15101/
reverse-a-named-url-from-view
<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="kn">from</span> <span class="nn">django.core.urlresolvers</span> <span class="kn">import</span> <span class="n">reverse</span> </span><span id="L-2"><a name="L-2"></a><span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;todo-incomplete_tasks&#39;</span><span class="p">,</span> \ </span><span id="L-3"><a name="L-3"></a> <span class="n">args</span><span class="o">=</span><span class="p">[</span><span class="n">task</span><span class="o">.</span><span class="n">list</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="n">task</span><span class="o">.</span><span class="n">list</span><span class="o">.</span><span class="n">slug</span><span class="p">]))</span> </span></pre></div> </td></tr></table>
django, urls, views