Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
How to check a string starts/ends with a specific string in PHP ?
Next article icon

How to check if a String contains a Specific Character in PHP ?

Last Updated : 06 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In PHP, determining whether a string contains a specific character is a common task. Whether you're validating user input, parsing data, or performing text processing, PHP provides several methods to check for the presence of a particular character within a string efficiently.

Here are some common approaches:

Table of Content

  • Using strpos( ) function
  • Using strstr( ) function
  • Using preg_match() function

Using strpos( ) function

The strpos() function in PHP finds the position of a specific character or substring within a string. If the character or substring is found, it returns the position; otherwise, it returns `false`. This helps check for presence.

Example: Implementation to check if a string contains a specific character using the strpos( ) function

PHP
<?php $string = "Hello, world!"; $char = "o";  // Using strpos() function if (strpos($string, $char) !== false) {     echo "The string contains the character '$char'."; } else {     echo "The string does not contain the character '$char'."; }  ?> 

Output
The string contains the character 'o'. 

Using strstr( ) function

The strstr() function in PHP checks if a specific substring exists within a string. It returns the portion of the string from the first occurrence of the substring to the end. If the substring is not found, it returns `false`, enabling substring validation.

Example: Implementation to check if a string contains a specific character using strstr( ) function.

PHP
<?php $string = "Hello, world!"; $char = "o";  // Using strstr() function if (strstr($string, $char) !== false) {     echo "The string contains the character '$char'."; } else {     echo "The string does not contain the character '$char'."; } ?> 

Output
The string contains the character 'o'. 

Using preg_match() function

The preg_match() function is used to perform a regular expression match. It allows for more complex pattern matching, including checking for the presence of specific characters using regular expressions.

Example: In this example we checks if a specific character exists in a given string using the preg_match() function. It outputs a message indicating whether the character is present or not.

PHP
<?php // Nikunj Sonigara $string = "Hello, world!"; $char = "o";   // Using preg_match() function if (preg_match('/' . preg_quote($char, '/') . '/', $string)) {     echo "The string contains the character '$char'."; } else {     echo "The string does not contain the character '$char'."; } ?> 

Output
The string contains the character 'o'.

Next Article
How to check a string starts/ends with a specific string in PHP ?

P

pankaj_gupta_gfg
Improve
Article Tags :
  • Web Technologies
  • PHP
  • WebTech-FAQs
  • PHP-QnA

Similar Reads

  • How to check if a String Contains a Substring in PHP ?
    Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role. MethodsBelow are the following methods by which we can check if a string contains a substr
    1 min read
  • How to get the position of character in a string in PHP ?
    In this article, we will get the position of the character in the given string in PHP. String is a set of characters. We will get the position of the character in a string by using strpos() function. Syntax: strpos(string, character, start_pos) Parameters: string (mandatory): This parameter refers t
    2 min read
  • How to find if an array contains a specific string in JavaScript/jQuery ?
    In order to Check if an array contains a specific string be done in many ways in Javascript. One of the most common methods to do this is by using the traditional for loop. In Javascript, we have several other modern approaches which help in finding a specific string in an array to iterate the array
    5 min read
  • How to read each character of a string in PHP ?
    A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. Here are some approaches to read each character of a string in PHP Table of Content Using str_split() method - The st
    4 min read
  • How to check a string starts/ends with a specific string in PHP ?
    In this article, we are going to learn how to check that a given string is starting or ending with a given specific string in PHP. We require the use of XAMPP, Netbeans to run this code. Input: "Geeks For Geeks"Starting with: "G"Output: "Geeks For Geeks Starts with String G"In PHP version 8+, there
    2 min read
  • How to replace multiple characters in a string in PHP ?
    A string is a sequence of characters enclosed within single or double quotes. A string can also be looped through and modifications can be made to replace a particular sequence of characters in it. In this article, we will see how to replace multiple characters in a string in PHP. Using the str_repl
    3 min read
  • How to get the last character of a string in PHP ?
    In this article, we will find the last character of a string in PHP. The last character can be found using the following methods. Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, i
    2 min read
  • JavaScript - How to Check if a String Contains Double Quotes?
    Here are the different methods to check if a string contains double quotes. 1. Using includes() MethodThe includes() method is the simplest way to check if a string contains a specific substring, including double quotes. This method returns true if the substring is found and false otherwise. [GFGTAB
    4 min read
  • How to get string between two characters in PHP ?
    A string is a sequence of characters stored in the incremental form in PHP. A set of characters, one or many can lie between any two chosen indexes of the string. The text between two characters in PHP can be extracted using the following two methods : Table of Content Using substr() Method: Using f
    4 min read
  • How to remove the first character of string in PHP?
    Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation: In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. r
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences