snipt

Ctrl+h for KB shortcuts
Notice: Snipt is moving away from free accounts on May 1st, 2014. Read more about the transition here.
#39058

PHP

User IP Address

1
2
3
4
5
<?php

echo $_SERVER['REMOTE_ADDR'];

?>
https://snipt.net/embed/cf61c2cf192cca308af374dfea924c0e/
https://snipt.net/raw/cf61c2cf192cca308af374dfea924c0e/
cf61c2cf192cca308af374dfea924c0e
php
PHP
5
2014-04-20T12:34:52
True
False
/api/public/snipt/39058/
user-ip-address
<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 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="k">echo</span> <span class="nv">$_SERVER</span><span class="p">[</span><span class="s1">&#39;REMOTE_ADDR&#39;</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="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
ip, php
#34757

PHP

Reformat Date

<?php echo date('d/m/Y',strtotime($date)); ?>
https://snipt.net/embed/fe90133af4f6350765c680ca380d2366/
https://snipt.net/raw/fe90133af4f6350765c680ca380d2366/
fe90133af4f6350765c680ca380d2366
php
PHP
1
2014-04-07T02:46:56
True
False
/api/public/snipt/34757/
reformat-date
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</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 class="k">echo</span> <span class="nb">date</span><span class="p">(</span><span class="s1">&#39;d/m/Y&#39;</span><span class="p">,</span><span class="nb">strtotime</span><span class="p">(</span><span class="nv">$date</span><span class="p">));</span> <span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
date, php
#23311

PHP

Toggle a variable value in a loop

1
2
3
4
5
6
7
<?php
foreach ($array_items as $array_item) {

    $toggle = ($toggle == 1 ? 0 : 1); 

}
?>
https://snipt.net/embed/a3be067405df2f3a1d23552c0ddf5708/
https://snipt.net/raw/a3be067405df2f3a1d23552c0ddf5708/
a3be067405df2f3a1d23552c0ddf5708
php
PHP
7
2014-04-20T12:35:57
True
False
/api/public/snipt/23311/
toggle-a-variable-value-in-a-loop
<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="k">foreach</span> <span class="p">(</span><span class="nv">$array_items</span> <span class="k">as</span> <span class="nv">$array_item</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a> <span class="nv">$toggle</span> <span class="o">=</span> <span class="p">(</span><span class="nv">$toggle</span> <span class="o">==</span> <span class="mi">1</span> <span class="o">?</span> <span class="mi">0</span> <span class="o">:</span> <span class="mi">1</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="p">}</span> </span><span id="L-7"><a name="L-7"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
loop, php, toggle
#22995

MySQL

CONCAT, SUBSTRING_INDEX and LIKE example

-- example SQL for substrings and concatenation
UPDATE url_alias SET keyword = CONCAT(keyword, '-', SUBSTRING_INDEX(query, '=', -1)) WHERE query LIKE 'category_id%';
https://snipt.net/embed/8d34e2ad7d236e728e7668cbf915e84c/
https://snipt.net/raw/8d34e2ad7d236e728e7668cbf915e84c/
8d34e2ad7d236e728e7668cbf915e84c
mysql
MySQL
2
2014-04-22T07:43:27
True
False
/api/public/snipt/22995/
concat-and-substring_index-example
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="c1">-- example SQL for substrings and concatenation</span> </span><span id="L-2"><a name="L-2"></a><span class="k">UPDATE</span> <span class="n">url_alias</span> <span class="kt">SET</span> <span class="n">keyword</span> <span class="o">=</span> <span class="nf">CONCAT</span><span class="p">(</span><span class="n">keyword</span><span class="p">,</span> <span class="s1">&#39;-&#39;</span><span class="p">,</span> <span class="nf">SUBSTRING_INDEX</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="s1">&#39;=&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">))</span> <span class="k">WHERE</span> <span class="n">query</span> <span class="k">LIKE</span> <span class="s1">&#39;category_id%&#39;</span><span class="p">;</span> </span></pre></div> </td></tr></table>
concatenation, like, mysql, substring
#22766

Text only

Price RegEx - preg_match/replace compatible

/^\d+\.\d{2}$/
https://snipt.net/embed/5e963835f1f17b1f5b2db263518790a2/
https://snipt.net/raw/5e963835f1f17b1f5b2db263518790a2/
5e963835f1f17b1f5b2db263518790a2
text
Text only
1
2014-04-18T15:31:15
True
False
/api/public/snipt/22766/
price-regex-preg_match
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a>/^\d+\.\d{2}$/ </span></pre></div> </td></tr></table>
float, preg_match, preg_replace, price, regex