snipt

Ctrl+h for KB shortcuts

PHP

Sanitize Strings

1
2
3
4
5
6
7
function sanitize($contents) {
	$contents = str_replace("\r\n","\n",$contents); //converts win newlines to nix
	$contents = str_replace("\n\n","\n",$contents); //removes double newlines
	$contents = str_replace("\t","",$contents); //removes tabs

	return $contents;
}
https://snipt.net/embed/5dacba8103cff03eb5136fc4e231861e/
/raw/5dacba8103cff03eb5136fc4e231861e/
5dacba8103cff03eb5136fc4e231861e
php
PHP
7
2019-06-22T09:41:29
True
False
False
Feb 08, 2014 at 01:53 PM
/api/public/snipt/111971/
sanitize-strings
<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="x">function sanitize($contents) {</span> </span><span id="L-2"><a name="L-2"></a><span class="x"> $contents = str_replace(&quot;\r\n&quot;,&quot;\n&quot;,$contents); //converts win newlines to nix</span> </span><span id="L-3"><a name="L-3"></a><span class="x"> $contents = str_replace(&quot;\n\n&quot;,&quot;\n&quot;,$contents); //removes double newlines</span> </span><span id="L-4"><a name="L-4"></a><span class="x"> $contents = str_replace(&quot;\t&quot;,&quot;&quot;,$contents); //removes tabs</span> </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a><span class="x"> return $contents;</span> </span><span id="L-7"><a name="L-7"></a><span class="x">}</span> </span></pre></div> </td></tr></table>
input, php, sanitize, string

Python

remove html tags from a string

1
2
3
4
5
6
7
8
import re
import urllib

data = urllib.urlopen( "http://www.evilsocket.net/" ).read() 

data = re.sub( r'<[^>]*>', ' ', data ).strip()

print data
https://snipt.net/embed/99456d0075c23dd950a8ea2385ab1235/
/raw/99456d0075c23dd950a8ea2385ab1235/
99456d0075c23dd950a8ea2385ab1235
python
Python
8
2019-06-26T18:40:04
True
False
False
/api/public/snipt/25767/
remove-html-tags-from-a-string
<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="kn">import</span> <span class="nn">re</span> </span><span id="L-2"><a name="L-2"></a><span class="kn">import</span> <span class="nn">urllib</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="n">data</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span> <span class="s2">&quot;http://www.evilsocket.net/&quot;</span> <span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a><span class="n">data</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span> <span class="s1">r&#39;&lt;[^&gt;]*&gt;&#39;</span><span class="p">,</span> <span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="n">data</span> <span class="p">)</span><span class="o">.</span><span class="n">strip</span><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="k">print</span> <span class="n">data</span> </span></pre></div> </td></tr></table>
html, python, sanitize, strip, strip_tags, tags