snipt

Ctrl+h for KB shortcuts

PHP

Regex: Date format

1
2
3
4
<?
// Date must match format: mm/dd/yyyy
if (!preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $due_date))
  echo "Error !";
https://snipt.net/embed/3e949a4ab92f4abbfa3cc0ef86099bc4/
/raw/3e949a4ab92f4abbfa3cc0ef86099bc4/
3e949a4ab92f4abbfa3cc0ef86099bc4
php
PHP
4
2019-07-10T23:22:06
True
False
False
/api/public/snipt/8292/
regex-date-format
<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="cp">&lt;?</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">// Date must match format: mm/dd/yyyy</span> </span><span id="L-3"><a name="L-3"></a><span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nb">preg_match</span><span class="p">(</span><span class="s1">&#39;/^\d{2}\/\d{2}\/\d{4}$/&#39;</span><span class="p">,</span> <span class="nv">$due_date</span><span class="p">))</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">echo</span> <span class="s2">&quot;Error !&quot;</span><span class="p">;</span> </span></pre></div> </td></tr></table>
date, php, regex

PHP

regex - remove 2 or more whitespaces

1
2
3
<?
// remove multiple whitespaces
$line1 = preg_replace("/\s{2,}/", " ", $line1);
https://snipt.net/embed/27c291d423974a40bcf4f0a747ec99f4/
/raw/27c291d423974a40bcf4f0a747ec99f4/
27c291d423974a40bcf4f0a747ec99f4
php
PHP
3
2019-06-28T15:45:18
True
False
False
/api/public/snipt/8206/
regex-remove-2-or-more-whitespaces
<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">// remove multiple whitespaces</span> </span><span id="L-3"><a name="L-3"></a><span class="nv">$line1</span> <span class="o">=</span> <span class="nb">preg_replace</span><span class="p">(</span><span class="s2">&quot;/\s{2,}/&quot;</span><span class="p">,</span> <span class="s2">&quot; &quot;</span><span class="p">,</span> <span class="nv">$line1</span><span class="p">);</span> </span></pre></div> </td></tr></table>
php, regex, whitespaces

PHP

regex to return only first set

1
2
3
4
5
<?
// extract just the first element
$firstset = ereg_replace("^(.*) abc", "\\1", $data); 

// note: \\1 is the same as javascript's $1
https://snipt.net/embed/9ec10d94562259c00f86e4a181bfa44f/
/raw/9ec10d94562259c00f86e4a181bfa44f/
9ec10d94562259c00f86e4a181bfa44f
php
PHP
5
2019-06-21T11:27:37
True
False
False
/api/public/snipt/8205/
regex-to-return-only-first-set
<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></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">// extract just the first element</span> </span><span id="L-3"><a name="L-3"></a><span class="nv">$firstset</span> <span class="o">=</span> <span class="nb">ereg_replace</span><span class="p">(</span><span class="s2">&quot;^(.*) abc&quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="se">\\</span><span class="s2">1&quot;</span><span class="p">,</span> <span class="nv">$data</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="c1">// note: \\1 is the same as javascript&#39;s $1</span> </span></pre></div> </td></tr></table>
php, regex

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-17T01:21:49
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