1. How To Discovery a String of Regular Expressions on PHP

For example, we would like to whether the string "must" in the string "gestiyawati"?
The simplest way is looping from beginning to end to release the results is found or not.
Okay, go directly to the listing program. an example is made in a simple PHP programming language, can be written:
$string = 'gestiyawati';$pattern = 'gesti';
if(($location = strpos($string, $pattern))) {  echo $location;}echo '<br />';if(($location = strstr($string, $pattern))) {  echo $location;}
this step just above serves to "the same string", not a pattern.

2. Regular Expression

Regular Expression, which is a method for finding a pattern in a string. Regex itself was also a string. Only the characters in the string are interpreted as a pattern and certain rules. For example, the string is preceded by a hat "^" means the string must be preceded by the characters following the hat pins.

Regex syntax in any programming language is different. In PHP, originally known ereg (), eregi (), ereg_replace (), and eregi_replace (), to take advantage of this regex. However, these functions can not be used anymore since PHP 5.3.0 as already applied again function better.

Currently you can use the function preg_match () to use a regex. Other functions available are preg_replace (), preg_filter (), preg_match_all (), and preg_split (). As shown in the picture. All located in the library pcre.php. PCRE stands for Perl Compatible Regular Expression, which means that this regex can be used also in the Perl programming language. For more details, you can study the documentation on its official website.

By default, the pattern to be searched should be enclosed by a slash character (slash), which serves as delimiters. The simplest example Regex to find whether there was a pattern of "FGH" in the string "abcdefghijk" as follows.

<?php
$string = 'Gestiyawati';if(preg_match("/awa/", $string)) {  echo 'String berisi pola awa';} else {  echo 'Tidak ada pola awa dalam string';}?>

3. Initial character
To search pattern "beginning with a string", then the regex used is a character hat "^". For example, if a string beginning with "abc" then its regex is as follows.

<?php
$string = 'gestiyawati';if(preg_match("/^ges/", $string)) {  echo 'String berawalan ges';} else {  echo 'Tidak berawalan ges';}?>
Hat pins followed abc character means that the string must be prefixed with abc. While the next slash mark indicates that the character does not need to be ignored thereafter.

4. Case-Sensitive
But the regex does not apply if one or more characters abc at the beginning of the string to be searched is a capital. Sometimes you just need to look for the character patterns, regardless of size. To ignore case sensitivity, you can add delimiters "/ i" behind regex, be as follows.


<?php
$string = 'gestiyawati';if(preg_match("/^GES/i", $string)) {  echo 'String berawalan ges';} else {  echo 'Tidak berawalan ges';}?>
Thus, whether or not the pattern of capital sought will have no effect.

5. character Final

Next is to look for the end of the string to form a specific pattern. To look for a pattern in the end of a string, there are 2 ways. The first way is to use a dollar sign "$". However, this method has a small gap to arise a bug in your program if it is not used correctly. For a dollar sign enter the endline "\ n" into a pattern that is searchable, so when looking for a pattern of "xyz" in the string "abcxyz" and "abcxyz \ n" will yield the same value both right.

Not to enter the endline in the pattern, you can use a regex backslash z "\ z". Examples of its use as follows.

<?php
$string = 'Gestiyawati';if(preg_match("/wati\z/i", $string)) {  echo 'Pola berakhiran wati ditemukan';} else {  echo 'Pola tidak ditemukan';}?>

Recall that the "/ i" just indicates that the regex case insensitive. The initial character of the string will also not be ignored as long as the string has the appropriate suffix.

6. meta Characters
To search pattern containing characters then, should be given a separate treatment. Meta character is a character that is used as a marker Regex. As with the previous example, one example of a meta character is a "^" and "$". In addition, several other characters are dot ".", Asterisk "*", plus "+" question mark "?", All kinds of brackets, pipe "|", and the backslash "\".

Because it is a meta character, this character will not be read as a pattern, but simply a sign operation. To put it as a kind of character that in the pattern, you must add the backslash "\" before meta characters. Also known as the backslash escape character.
For example, if looking for patterns 2 + 7 in a string, it can be written as follows.

<?php
$string = '2+7=9';if(preg_match("/^2\+7/i", $string)) {  echo 'Pola 2+7 ditemukan';}else {  echo 'Tidak ditemukan';}?>

Note that before the plus sign also included a backslash. This also applies to all meta characters. Exceptions to the backslash character itself because the escape character also needs to be escaped as well, to include a backslash in the regex pattern, then it must be repeated four times to "\\\\".

7. Bracket

Characters brackets "[]" is a regex to declare a character class, which is a collection of characters desired. The characters can be written [abcdefg] or with a dash [a-g] special character sequence. To see what the corresponding character, you can use the function preg_match_all () and enter the third parameter in the form of a variable that will store any sub-strings that match the specified pattern.

<?php
$string = 'abcefghijklmnopqrstuvwxyz0123456789';
preg_match_all("/[a-f]/", $string, $matches);
foreach($matches[0] as $value) {  echo $value . '<br />';}?>

Almost all meta characters will not work as meta characters if within these brackets, but only became a regular character. So if you want to look for patterns that contain the letters a, b, c, and $, then simply write [abc $] alone, without the escape character.

Exceptions to the character hat "^". If placed at the beginning of the brackets, it means that you look for is character that is not in the square brackets. For example, if you want to look for patterns in addition to the letter "bgjk" in the string, then it should be written as follows.

<?php
$string = 'gestiyawati';
preg_match_all("/[^gea]/", $string, $matches);
foreach($matches[0] as $value) {  echo $value . '<br />';}
?>

8. Point
Point is also a meta character which means it will return true for all the characters other than line breaks (\ r and \ n), but only one character. Examples are as follows.

<?php
$string = 'gesti gestia gea gestiyawati';if(preg_match_all("/g.ea/", $string, $match)) {  foreach($match[0] as $m) {    echo $m . '<br />';  }}?>

The program will print the word "helo", "hilo", "h lo", and "hxlo", without the last word. To insert a line break in the rules, you have to give flag "\ s" at the end of the regex, just like the way the writing case insensitive.

9. Star and Add

An asterisk "*" and added "+" have a similar meaning. In regex, an asterisk is placed after a character, which means that the character can be zero or more looping expected in the pattern. As for the sign, it means that there must be one or more (must not be zero) characters written in the rules. Suppose, in the example program below.

<?php
$string = array('rby', 'ruby', 'ruuuby');foreach($string as $s) {  if(preg_match("/ru*by/", $s)) {    echo 'Match ru*by '.$s.'<br />';  }  if(preg_match("/ru+by/", $s)) {    echo 'Match ru+by '.$s.'<br />';  }}?>

The program will produce the output:

Match ru*by rby
Match ru*by rubyMatch ru+by rubyMatch ru*by ruuubyMatch ru+by ruuuby

It is apparent that the first string (RBY) will not be accepted by the regex sign, but only asterisks. A plus sign after the letter u mean that at least one letter 'u'.

10. Question mark
A question mark "?" Is a meta character for regex rule which means that character is optional, there may be no. One example of the use of this code is to detect telephone numbers. Sometimes there is enter the area code with minux separator character "-", sometimes there is not. For example, in the following program.

<?php
$telp = '0857-111111';if(preg_match("/0857-?111111/", $telp)) {  echo 'Nomer telp benar';} else {  echo 'Nomer telp salah';}?>

Even if the dash is deleted, the pattern is still accepted by the regex because of the minus sign is optional.

11. Curly brackets
Curly brackets "{}" is a meta character which symbolizes the number of characters or patterns that should be there. Usually braces is included after the brackets, which means there must be some characters contained in brackets, some numbers written in curly brackets. For example, suppose a phone number must be included patterns of zeroes as many as three times, the writing program as follows.

<?php
$telp = '0341000123';if(preg_match("/03410{3}123/", $telp)) {  echo 'Nomer telp benar';} else {  echo 'Nomer telp salah';}?>

You can also combine it with other meta characters. For example, to detect a home phone number format Malang consisting of area code 0341, followed by 6 the next number, then its regex is as follows.

<?php
$telp = '0341-000123';if(preg_match("/0341-?[0-9]{6}$/", $telp)) {  echo 'Nomer telp Malang';} else {  echo 'Nomer telp salah';}?>

To create a rule that at least some x looping, then you can write it down followed by a comma after the number expected. For example, if you want there are at least 3-digit numbers, it can be written as "[0-9] {3}". Whereas when the number of iteration wants the expected range, you can include additional numbers after the comma, as {3,6} which means that the number of loops should be between 3 to 6 times.

12. Sequence Special Characters
Alphabet and numbers is a special character sequence. Quite inconvenient not having to write [dac until xyz] or [a-z]? Just like other formats, the regex escape character is also known on the alphabet, numbers, and linebreak. The following is a chronological list of special characters that apply to the regex.

\ D - For the entire character is a number, just like [0-9]
\ D - For all non-numeric characters, the same as [^ 0-9]
\ S - For the whitespace characters, like writing [\ t \ r \ n \ f \ v]
\ S - For non-whitespace characters, like writing [^ \ t \ r \ n \ f \ v]
\ W - For all the alpha-numeric characters, the alphabet and numbers, the same as [a-zA-Z0-9_]
\ W - For all non alpha-numeric characters, the same as [^ a-zA-Z0-9_]
Examples of its use as follows. Suppose you're looking for a string with a pattern that preceded a single digit, then three letters, and topped off by a number or letter. So, writing is:

<?php
$string = '2hgd3';if(preg_match("/^\d[A-Za-z]{3}\w$/", $string)) {  echo 'Pola sesuai';}?>

13. Ordinary brackets
Just as in mathematics, parentheses "()" are used to other regex rules. Innermost parentheses will be processed first, then the new brackets are welcome. Its use can vary, you can see in the examples hereinafter.

14. Pipe character
Pipe character or vertical line "|" declared the operation "or". When coupled with the brackets, you can specify the string there must be a certain pattern. Suppose, format only accepts regional telephone numbers Jakarta, Surabaya, and Malang, with six or seven digit number. Then you can write it as follows.

<?php
$string = array('0341-000123', '0311231231',  '021999999', '041321321', '031-12332');foreach($string as $s) {  if(preg_match("/^(0341|031|021)-?[\d]{6,7}$/", $s)) {    echo 'Nomer '.$s.' sesuai<br />';  } else {    echo 'Nomer '.$s.' tidak sesuai<br />';  }}?>

Results regex above, number 1 through the 3rd an appropriate pattern. 4th number is not appropriate because it does not begin the area code specified. Number 5 is not appropriate as it contains only 5-digit number after the area code.

15. Modifiers and Assertions
In the previous example, you have learned the modifier "i" to state that regex used are case insensitive. Here are some other modifiers that you can use as a case that you need.

i - ignore capitalization or case insensitive
U - regex search with the method ungreedy
s - include a new line in the pattern
m - allows patterns in different lines
x - for regex with comments and whitespace
E - evaluation of php code (special function preg_replace)
S - additional analysis of the patterns
Modifier should be written after the delimiters cover on the right regex.

For there are also some regex assertion that can be used.

The character "\ b" means as a word boundary, or limitation of a word. This is to prevent the pattern is sought as a substring of another word. For example, for the word "safe" in the sentence "This page has been safe", then it should be written as follows.

<?php
$string = 'halaman ini telah aman';if(preg_match("/\baman\b/", $string)) {  echo 'Pola ditemukan';}?>

Other assertions are:

\ B - not a word boundary character
\ A - the beginning of the subject
\ Z - end of subject, or new line character
\ Z - end of subject
\ G - the corresponding position at the beginning of the subject
All assertions must be preceded by the escape character, to indicate that the use is assertions.

16. Delimiter
In the previous examples, delimiter used is slash "/". Delimiters can also be reimbursed for certain cases, especially when you need the slash character in the pattern sought. It can use the escape character, but if enough slash marks are needed as in the URL pattern, it will be quite a hassle.
Several other delimiters that can be used is /, @, #, `(no quotes), ~,%, &, quotes one and two. Thus, you can also write it as follows.

preg_match("#asdf#", $string);

17. preg_match ( "# asdf # ', $ string);

Viewing Patterns in the Front and Back
Regex has the ability to find patterns to look backward and forward of the pattern is sought. For example, want to just look for the word "name" is in front followed by a colon. Or want to find the word "media" that begins with the word "pc". All can be done with regex.
To see the characters or words in front of the search term, you can use a regex "(? =)". Consider the following example.

<?php
$string = 'pcmedia';if(preg_match("/pc(?=media)/", $string)) {  echo 'Pattern ditemukan';} else {  echo 'Pattern tidak ditemukan';}?>

The program will be true. because the character "pc" immediately followed by "media". Try to change the word came to have a space, then it would be worth one.
To find a pattern after the search term, but yielded negative results, using regex "(?!)". That is, you are looking for is a word that is not followed by another word in the regex. Consider the following example.

<?php
$string = 'pcmedia';if(preg_match("/pc(?!media)/", $string)) {  echo 'Pattern ditemukan';} else {  echo 'Pattern tidak ditemukan';}?>


Then that will come out is a wrong statement. This is because there is no word "pc" that is not followed by the "media", while there is "PCMedia" directly.
To look back, regex used is "(? <=)". Same goal, look for patterns with prefix other patterns. Examples are as follows.

<?php
$string = 'pcmedia';if(preg_match("/(?<=pc)media/", $string)) {  echo 'Pattern ditemukan';} else {  echo 'Pattern tidak ditemukan';}?>

As for the search backwards, but that is sought is a negative value, then use a regex "(? <!)". So to be searched is a pattern of characters that do not follow the pattern in question. Consider the following example.

<?php
$string = 'pcmedia';if(preg_match("/(?<!pc)media/", $string)) {  echo 'Pattern ditemukan';} else {  echo 'Pattern tidak ditemukan';} ?>

The above code will not find a pattern that is sought, since the string is only the word "media" beginning with "pc". It should be sought by the pattern is "media" without the prefix "pc".

function of
and again guys...

19. Email pattern

After studying all the basic regex in PHP, you can put them together into one way of finding the complex patterns such as email addresses. There are many regex that can be implemented, depending on the mindset of each programmer. Here is one of the regex used to detect whether the address is an email address is valid or not.

<?php
$email = 'me@haqqi.net';if(preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)) {  echo 'Email valid';} else {  echo 'Email tidak valid';}?>

just reminder me :) 

Source code and full copied posting by:


Thanks To:
(Candra Ikchsan)

Author by me (just copied):
(Gestia)