snipt

Ctrl+h for KB shortcuts

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