Regular Expressions in PHP

Last updated on May 31 2022
Aridam Das

Table of Contents

Regular Expressions in PHP

Regular expressions are nothing more than a sequence or pattern of characters itself. They provide the foundation for pattern-matching functionality.

Using regular expression you can search a particular string inside a another string, you can replace one string by another string and you can split a string into many chunks.

PHP offers functions specific to two sets of regular expression functions, each corresponding to a certain type of regular expression. You can use any of them based on your comfort.

  • POSIX Regular Expressions
  • PERL Style Regular Expressions

POSIX Regular Expressions

The structure of a POSIX regular expression is not dissimilar to that of a typical arithmetic expression: various elements (operators) are combined to form more complex expressions.

The simplest regular expression is one that matches a single character, such as g, inside strings such as g, haggle, or bag.

Lets give explanation for few concepts being used in POSIX regular expression. After that we will introduce you with regular expression related functions.

Brackets

Brackets ([]) have a special meaning when used in the context of regular expressions. They are used to find a range of characters.

Sr.No Expression & Description
1 [0-9]

It matches any decimal digit from 0 through 9.

2 [a-z]

It matches any character from lower-case a through lowercase z.

3 [A-Z]

It matches any character from uppercase A through uppercase Z.

4 [a-Z]

It matches any character from lowercase a through uppercase Z.

The ranges shown above are general; you could also use the range [0-3] to match any decimal digit ranging from 0 through 3, or the range [b-v] to match any lowercase character ranging from b through v.

Quantifiers

The frequency or position of bracketed character sequences and single characters can be denoted by a special character. Each special character having a specific connotation. The +, *, ?, {int. range}, and $ flags all follow a character sequence.

Sr.No Expression & Description
1 p+

It matches any string containing at least one p.

2 p*

It matches any string containing zero or more p’s.

3 p?

It matches any string containing zero or one p’s.

4 p{N}

It matches any string containing a sequence of N p’s

5 p{2,3}

It matches any string containing a sequence of two or three p’s.

6 p{2, }

It matches any string containing a sequence of at least two p’s.

7 p$

It matches any string with p at the end of it.

8 ^p

It matches any string with p at the beginning of it.

Examples

Following examples will clear your concepts about matching characters.

Sr.No Expression & Description
1 [^a-zA-Z]

It matches any string not containing any of the characters ranging from a through z and A through Z.

2 p.p

It matches any string containing p, followed by any character, in turn followed by another p.

3 ^.{2}$

It matches any string containing exactly two characters.

4 <b>(.*)</b>

It matches any string enclosed within <b> and </b>.

5 p(hp)*

It matches any string containing a p followed by zero or more instances of the sequence php.

Predefined Character Ranges

For your programming convenience several predefined character ranges, also known as character classes, are available. Character classes specify an entire range of characters, for example, the alphabet or an integer set −

Sr.No Expression & Description
1 [[:alpha:]]

It matches any string containing alphabetic characters aA through zZ.

2 [[:digit:]]

It matches any string containing numerical digits 0 through 9.

3 [[:alnum:]]

It matches any string containing alphanumeric characters aA through zZ and 0 through 9.

4 [[:space:]]

It matches any string containing a space.

PHP’s Regexp POSIX Functions

PHP currently offers seven functions for searching strings using POSIX-style regular expressions −

Sr.No Function & Description
1 ereg()

The ereg() function searches a string specified by string for a string specified by pattern, returning true if the pattern is found, and false otherwise.

2 ereg_replace()

The ereg_replace() function searches for string specified by pattern and replaces pattern with replacement if found.

3 eregi()

The eregi() function searches throughout a string specified by pattern for a string specified by string. The search is not case sensitive.

4 eregi_replace()

The eregi_replace() function operates exactly like ereg_replace(), except that the search for pattern in string is not case sensitive.

5 split()

The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.

6 spliti()

The spliti() function operates exactly in the same manner as its sibling split(), except that it is not case sensitive.

7 sql_regcase()

The sql_regcase() function can be thought of as a utility function, converting each character in the input parameter string into a bracketed expression containing two characters.

PERL Style Regular Expressions

Perl-style regular expressions are similar to their POSIX counterparts. The POSIX syntax can be used almost interchangeably with the Perl-style regular expression functions. In fact, you can use any of the quantifiers introduced in the previous POSIX section.

Lets give explanation for few concepts being used in PERL regular expressions. After that we will introduce you wih regular expression related functions.

Meta characters

A meta character is simply an alphabetical character preceded by a backslash that acts to give the combination a special meaning.

For instance, you can search for large money sums using the ‘\d’ meta character: /([\d]+)000/, Here \d will search for any string of numerical character.

Following is the list of meta characters which can be used in PERL Style Regular Expressions.

Character                              Description

.                                      a single character

\s                                     a whitespace character (space, tab, newline)

\S                                     non-whitespace character

\d                                     a digit (0-9)

\D                                     a non-digit

\w                                     a word character (a-z, A-Z, 0-9, _)

\W                                     a non-word character

[aeiou]                                matches a single character in the given set

[^aeiou]                               matches a single character outside the given set

(foo|bar|baz)                          matches any of the alternatives specified

Modifiers

Several modifiers are available that can make your work with regexps much easier, like case sensitivity, searching in multiple lines etc.

Modifier       Description

i              Makes the match case insensitive

m              Specifies that if the string has newline or carriage               

               return characters, the ^ and $ operators will now               

               match against a newline boundary, instead of a              

               string boundary

o              Evaluates the expression only once

s              Allows use of . to match a newline character

x              Allows you to use white space in the expression for clarity

g              Globally finds all matches

cg             Allows a search to continue even after a global match fails

PHP’s Regexp PERL Compatible Functions

PHP offers following functions for searching strings using Perl-compatible regular expressions −

Sr.No Function & Description
1 preg_match()

The preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise.

2 preg_match_all()

The preg_match_all() function matches all occurrences of pattern in string.

3 preg_replace()

The preg_replace() function operates just like ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters.

4 preg_split()

The preg_split() function operates exactly like split(), except that regular expressions are accepted as input parameters for pattern.

5 preg_grep()

The preg_grep() function searches all elements of input_array, returning all elements matching the regexp pattern.

6 preg_ quote()

Quote regular expression characters

 

So, this brings us to the end of blog. This Tecklearn ‘Regular Expressions in PHP’ blog helps you with commonly asked questions if you are looking out for a job in PHP Programming. If you wish to learn Java and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/java-and-jee-training/

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

  • Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
  • Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
  • Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

  • Java Fundamentals
  • Introduction to Java Basics
  • Features of Java
  • Various components of Java language
  • Benefits of Java over other programming languages
  • Key Benefits of Java

Installation and IDE’s for Java Programming Language

  • Installation of Java
  • Setting up of Eclipse IDE
  • Components of Java Program
  • Editors and IDEs used for Java Programming
  • Writing a Simple Java Program

Data Handling and Functions

  • Data types, Operations, Compilation process, Class files, Loops, Conditions
  • Using Loop Constructs
  • Arrays- Single Dimensional and Multi-Dimensional
  • Functions
  • Functions with Arguments

OOPS in Java: Concept of Object Orientation

  • Object Oriented Programming in Java
  • Implement classes and objects in Java
  • Create Class Constructors
  • Overload Constructors
  • Inheritance
  • Inherit Classes and create sub-classes
  • Implement abstract classes and methods
  • Use static keyword
  • Implement Interfaces and use it

Polymorphism, Packages and String Handling

  • Concept of Static and Run time Polymorphism
  • Function Overloading
  • String Handling –String Class
  • Java Packages

Exception Handling and Multi-Threading

  • Exception handling
  • Various Types of Exception Handling
  • Introduction to multi-threading in Java
  • Extending the thread class
  • Synchronizing the thread

File Handling in Java

  • Input Output Streams
  • io Package
  • File Handling in Java

Java Collections

  • Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
  • Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
  • Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

  • Introduction to SQL: Connect, Insert, Update, Delete, Select
  • Introduction to JDBC and Architecture of JDBC
  • Insert/Update/Delete/Select Operations using JDBC
  • Batch Processing Transaction
  • Management: Commit and Rollback

Java Enterprise Edition – Servlets

  • Introduction to J2EE
  • Client Server architecture
  • URL, Port Number, Request, Response
  • Need for servlets
  • Servlet fundamentals
  • Setting up a web project in Eclipse
  • Configuring and running the web app with servlets
  • GET and POST request in web application with demo
  • Servlet lifecycle
  • Servlets Continued
  • Session tracking and filter
  • Forward and include Servlet request dispatchers

Java Server Pages (JSP)

  • Fundamentals of Java Server Page
  • Writing a code using JSP
  • The architecture of JSP
  • JSP Continued
  • JSP elements: Scriptlets, expressions, declaration
  • JSP standard actions
  • JSP directives
  • Introduction to JavaBeans
  • ServletConfig and ServletContext
  • Servlet Chaining
  • Cookies Management
  • Session Management

Hibernate

  • Introduction to Hibernate
  • Introduction to ORM
  • ORM features
  • Hibernate as an ORM framework
  • Hibernate features
  • Setting up a project with Hibernate framework
  • Basic APIs needed to do CRUD operations with Hibernate
  • Hibernate Architecture

POJO (Plain Old Java Object)

  • POJO (Plain Old Java Object)
  • Persistent Objects
  • Lifecycle of Persistent Object

Spring

  • Introduction to Spring
  • Spring Fundamentals
  • Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Regular Expressions in PHP"

Leave a Message

Your email address will not be published. Required fields are marked *