Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

15. Regular Expressions

Lesson 12 of 29 in the free PHP Programming notes on Siksha Sarovar, written by Rohit Jangra.

Regular Expressions in PHP

A Regular Expression (RegEx) is a pattern used to search, match, or validate text.

Why Regular Expressions are Used?

  • Validate form inputs
  • Check email format
  • Validate phone numbers
  • Search patterns in strings
Exam Definition: Regular expressions are patterns used for matching and validating strings.

---

PHP RegEx Functions

PHP uses PCRE (Perl Compatible Regular Expressions).

FunctionPurpose
preg_match()Match pattern
preg_match_all()Match all occurrences
preg_replace()Replace text
preg_split()Split string

---

Validating Text Boxes (Name Validation)

Only Alphabets Allowed

$name = "Rohit";

if(preg_match("/^[a-zA-Z ]+$/", $name)){
   echo "Valid Name";
}else{
   echo "Invalid Name";
}
Explanation ^ start $ end [a-zA-Z ] allowed characters + one or more characters

---

Validating Email Address

$email = "test@gmail.com";

if(preg_match("/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/", $email)){
   echo "Valid Email";
}else{
   echo "Invalid Email";
}
Real-time Use: Login and registration forms.

---

Validating Phone Number

10-Digit Mobile Number

$phone = "9876543210";

if(preg_match("/^[0-9]{10}$/", $phone)){
   echo "Valid Phone Number";
}else{
   echo "Invalid Phone Number";
}

---

Validating Password

$password = "Test@123";

if(preg_match("/^(?=.*[A-Za-z])(?=.*d)(?=.*[@$!%*#?&]).{6,}$/", $password)){
   echo "Strong Password";
}else{
   echo "Weak Password";
}

---

Creating Custom Regular Expressions

Custom RegEx are created using symbols and patterns.

Common RegEx Symbols

SymbolMeaning
^Start of string
$End of string
.Any character
*Zero or more
+One or more
?Optional
{n}Exact count
{n,m}Range
[]Character set
\dDigit
\wWord character

Custom Example: Username Validation

Rules

  • Letters and numbers only
  • Length 5 to 10
$username = "user123";

if(preg_match("/^[a-zA-Z0-9]{5,10}$/", $username)){
   echo "Valid Username";
}else{
   echo "Invalid Username";
}

---

Advantages of Regular Expressions

  • Fast validation
  • Less code
  • Accurate matching
  • Secure input handling