PHP tutorial-Special characters

Regular Expression

Special characters

To write more complex string patterns, you need to know how to use special characters in the string patterns. The special characters and examples are listed below:

Special Character Description
\dMatches any digit character from 0 to 9. Example: /\d\d\d/ matches 123 but not 12a ore a12,...
\DMatches any character that is not a digit. Example: /\D/ matches A but not 1.
\sMatches any character that is a whitespace including tab, carriage return, form feed, new line, and vertical tab. Example: /\s/ matches tab.
\SMatches any character that is not a whitespace character. Example: /\S/ matches A but not tab.
\w Matches any character from a to z, A to Z, and underscore. Example: /\w/ match _ but not 0.
\W Matches any character that is not from a to z, A to Z, 0 to 9, and underscore. Example: /\W/ matches @ but not a.
[...] Matches any character that is in  brackets. Example: /[ab]/ matches a or b only. [a-z] matches any character alphabetically from a to z. [a-zA-Z] matches a lower case or upper case letter alphabetically from a to z or from A to Z.
[^...] Matches any character that is not in bracket. Example:/ [^ab]/ matches A or B but not a or b.
.Matches any character that is not a new line. Example /./ matches a,0, or %
?Matches the previous item 0 time or 1 time. Example: /J?/ matches J or not at all.
*Matches the previous item 0 time or more times. Example: /J*/ matches J, JJ, JJJ, or not at all
+Matches the previous item 1 time or more times. Example: /J+/ matches J,JJ, or JJJ.
{n}Matches the previous item n times. Example:/\d{2}/ matches 34, 44, or 89.
{n,}Matches the previous item more than n times. Example: /\d{1,}/ matches 0, 10,287,3897, or 90000.
{n,m}Matches the previous item from n to m times. Example: /\d{1,3}/ matches 1, 23, or 897.
^Matches a pattern at the beginning of a string.
$Matches a pattern at the end of a string.

For example, now you want a user to enter his/her phone number in a text box in the following example format: (855)89-256-724. To check the phone number validity(conforming to this format), your pattern should be:

To help you understand the pattern, we break it in to small parts:
[(] and [)] match opening and closing parentheses.
/855/ matches 855.
/\d{2}/ matches any two digits.
/-/ matches the minus sign.
/\d{3}/ matches any three digits.
Note: Backward slash(\) is an escape character used to escape special characters.

Now let see this task is done on the browser by executing the code:

<title>Phone checking</title>
if(preg_match($strpat, $ph)==1) header("location: welcome.php");
else echo "<font color='red'>Invalid!</font>";
<form action="phonecheck.php" method="GET">
Enter phone number: <input type="text" name="phone" /><br />
<input type="submit" value="Submit">

echo "Welcome!";

Alternative Group of Characters
You can group more than one expressions together. For example, you have a string like "PHP, and JavaScript Programming languages are the same?". How can you write a pattern to match PHP or JavaScript in that string? This kind of problem can be solved by grouping PHP and JavaScript(separated by | sign) patterns together in parentheses: $strpat='/(PHP|JavaScript)/';

$str="PHP, JavaScript Programming languages are the same?";
echo $strnew;


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.