For another example, if you want to allow a user to enter a phone number in the format: (855)987-678. It is also hard to complete this task by using what you have learnt with the string functions.
PHP provides preg_match() and preg_replace() functions that you can solve these such problems. By using a regular expression, you can define a complex pattern of string to be matched.
For example, to solve the first problem, you can write a string pattern as below:
Then supply this string variable to the preg_replace(pattern, replacement,string) function.
A string pattern of regular expression is written between the forward slash(/pattern/) to inform the browsers that it is regular expression.
You may feel hard to write a pattern right now. In the next pages of Regular Expression topic you learn more things including how to write patterns and use them with the string manipulation functions mentioned previously.
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: