title: Regular expressions and matching
body: <p>Here is a list of examples of PHP regular expressions examples. Maybe this helps someone, as admin/ or another user can't make clear that I was trying to share my approaches.</p>
  
  <p><a href="https://www.php.net/manual/en/function.preg-match.php" rel="nofollow noreferrer">preg_match</a> does the search (<a href="https://www.php.net/manual/en/function.preg-replace.php" rel="nofollow noreferrer">preg_replace</a> is a replacer).</p>
  
  <p>preg_match has three parameters - preg_match(FindWhat, FindWhere, GivingOutput);</p>
  
  <p><strong>Example 1)</strong>:</p>
  
  <pre><code>&lt;?php
      // Everything expect letters and numbers
      $text = 'abc345fg@h';
      $newfilename = preg_match('/[^a-zA-Z0-9.]/', $text, $out);
      echo $out[0];
  ?&gt;
  Output will be:
  @
  </code></pre>
  
  <p>preg_match finds only one result (the firstly found result), with two options: [0] or <a href="https://www.php.net/manual/en/function.preg-match.php" rel="nofollow noreferrer">1</a>.</p>
  
  <p><strong>Example 2)</strong>: find everything (any characters,words..) inside our search criteria:</p>
  
  <pre><code>&lt;?php
      $text = 'abcdefghijklmnopqrst';
      $newfilename = preg_match('/ij(.*?)mn/', $text, $out);
      echo $out[0];
      echo $out[1];
  ?&gt;
  [1] -gives only the inner search result (what we had in the brackets,  between "ij" and "mn"):
  kl
  
  [0] -gives the whole search result:
  ijklmn
  </code></pre>
  
  <p>(Note, that option <a href="https://www.php.net/manual/en/function.preg-match.php" rel="nofollow noreferrer">1</a> is not available if when you don’t use brackets in search criteria (as we have above, in example 1)</p>
  
  <p><strong>Example 3)</strong>:</p>
  
  <p>If your target text has many same occurrences, like this:
  $text = 'Hello user <strong>Jimmy Jones</strong>, it\'s me. Hello user <strong>Mery Pawders</strong>, it\'s still me.';</p>
  
  <p>Now, here are two different matches, so, we need to use <a href="https://www.php.net/manual/en/function.preg-match-all.php" rel="nofollow noreferrer">preg_match_all</a></p>
  
  <pre><code>&lt;?php
      $text = 'Hello user Jimmy Jones, it\'s me. Hello user Mery Pawders, it\'s me.';
      $newfilename = preg_match_all('/hello user (.*?) it\'s/', $text, $out);
      foreach ($out[1] as $found_one) {
          echo $found_one;
      }
      // Or use $out[0] for full search match
  ?&gt;
  
  Output will be:
  Jimmy Jones,
  Mery Pawders,
  </code></pre>
  
  <p><strong>Example 4)</strong>: search among many possibilities:</p>
  
  <pre><code>&lt;?php
      $text = 'member ACCOUNT7';
      preg_match("/ACCOUNT[123456789]/", $text, $out);
      echo $out[1];
  ?&gt;
  
  Output will be:
  ACCOUNT7
  </code></pre>
  
  <p><strong>Example 5)</strong>: To find a string, while input text contains new lines, you must use<strong>s</strong> at the end;</p>
  
  <pre><code>&lt;?php
      $text = 'one
      two
      three';
      preg_match("/one(.*?)three/s", $text, $out);
      echo $out[1];
  ?&gt;
  
  Output will be:
  two
  </code></pre>
  
  <p><strong>Example 6)</strong>: Your search is always case sensitive. To make a case <em>insensitive</em> search, use <strong>i</strong> at the end (if you want, without <strong>s</strong>);</p>
  
  <pre><code>&lt;?php
      $text = 'ONE TWO TREE';
      preg_match("/one(.*?)three/si", $text, $out);
      echo $out[1];
  ?&gt;
  </code></pre>
  
  <p><strong>Example 7)</strong>: to search for <strong>special characters</strong> (like /".&lt;*'?, etc.) inside preg_match, you need to use this escape sign: \</p>
  
  <pre><code>&lt;?php
      $text = 'hello Jimmy/Kroger ';
      preg_match("/Jimmy\/Kroger/", $text, $out);
      echo $out[0];
  ?&gt;
  </code></pre>
  
  <p>Now, we can use the ^ operator, which searches for results conversely.</p>
  
  <p><strong>Example 8)</strong>: find everything rather than letters and numbers:</p>
  
  <pre><code>&lt;?php
      $text = 'abc@*&amp;^)($%';
      preg_match_all('/[^a-zA-Z0-9.]/', $text, $out);
      foreach ($out[0] as $varr) {
          echo $varr;
      }
  ?&gt;
  Output will be:
  @*&amp;^)($%
  </code></pre>
  
  <p>For <strong><em>search</em></strong> and <strong><em>replace</em></strong>, we have a bit different structure, as we need to use the new variable.</p>
  
  <p><strong>Example 9)</strong>: find and replace everything <strong>rather than</strong> letters and numbers with other character, using this operator: ^</p>
  
  <pre><code>&lt;?php
      $text = 'ab2sq)(&amp;*(%$%^$@%n23f9';
      $variable = preg_replace('/[^a-zA-Z0-9.]/', 'a', $text);
      echo $variable;
  ?&gt;
  Output will be:
  ab2sqn23f9
  </code></pre>
  
  <p><strong>Example 10)</strong>:  search and add something inside the found results:</p>
  
  <pre><code>&lt;?php
      $text = 'Hi, it\'s me, Niko from Austria';
      $variable = preg_replace('/(Niko.*?) from/', '$1 Gomez', $text);
      echo $variable;
  ?&gt;
  Output will be:
  
  it's me, Niko Gomez Austria
  </code></pre>
  
  <p><strong>Example 11)</strong>: find all links inside text:</p>
  
  <pre><code>&lt;?php
      $text = 'hi, my site is http://example.com, and on my page, at http://example.com/page37/blabla.html I wrote something..';
      preg_match_all("/[[:alpha:]]+:\/\/[^&lt;&gt;[:space:]]+[[:alnum:]\/]/",$text, $out);
      foreach($out[0] as $varr){
          echo $varr;
      }
  ?&gt;
  Output will be:
  http://example.com
  http://example.com/page37/blabla.html
  </code></pre>
  
  <p><strong>Example 12)</strong>: like the example 11 (but with replace) - find links in text and put them in anchored tags:</p>
  
  <pre><code>&lt;?php
      $text = 'Hi, my site is http://example.com, and on my page, at http://example.com/page37/trid.html I wrote something..';
      $variable = preg_replace("/[[:alpha:]]+:\/\/[^&lt;&gt;[:space:]]+[[:alnum:]\/]/",'&lt;a href="\\0"&gt;\\0&lt;/a&gt;', $text);
      echo $variable;
  ?&gt;
  </code></pre>
  
  <p>Output will be the same sentence, but the links will be anchored.</p>
  
  <p>1) <em>Tips: Do not use preg_match() if you only want to check if one string is contained in another string. Use stristr() or strpos() instead as they will be faster.</em></p>
  
  <p>2) **More advanced, specific examples about PHP regular expressions, use Google, or see **
  <em>full options and manuals</em> at - <a href="http://www.php.net/manual/en/reference.pcre.pattern.syntax.php" rel="nofollow noreferrer">http://www.php.net/manual/en/reference.pcre.pattern.syntax.php</a></p>
  
  <p>(You can review shortly all operators list here -<br/>
   <a href="http://www.catswhocode.com/blog/15-php-regular-expressions-for-web-developers" rel="nofollow noreferrer">http://www.catswhocode.com/blog/15-php-regular-expressions-for-web-developers</a><br/>
  <a href="http://www.noupe.com/php/php-regular-expressions.html" rel="nofollow noreferrer">http://www.noupe.com/php/php-regular-expressions.html</a><br/>)</p>
  
  <p>3) For HTML codes, there exist special light, PHP software, called- DOM Parser. But sometimes, if you know PHP regular expressions well, you might not need a DOM parser.</p>