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( ) functionUsing strstr( ) functionUsing preg_match() functionUsing strpos( ) functionThe 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'."; } ?> OutputThe string contains the character 'o'. Using strstr( ) functionThe 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'."; } ?> OutputThe string contains the character 'o'. Using preg_match() functionThe 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'."; } ?> OutputThe string contains the character 'o'. Comment More infoAdvertise with us Next Article How to check if a String contains a Specific Character in PHP ? P pankaj_gupta_gfg Follow Improve Article Tags : 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 substri 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 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 PHPTable of ContentUsing str_split() method - The str_ 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 ar 2 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 ContentUsing substr() Method: Using fo 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. re 3 min read PHP Program to Check Whether a Character is a Vowel or Consonant There are a total of 26 letters in the English alphabet. There are 5 vowel letters and 21 consonant letters. The 5 vowel letters are: "a", "e", "i", "o", and "u" rest are consonants. There are different approaches to checking whether a character is a vowel or a consonant. These are the different App 3 min read PHP Program to check a string is a rotation of another string Given the two strings we have to check if one string is a rotation of another string. Examples: Input : $string1 = "WayToCrack", $string2 = "CrackWayTo"; Output : Yes Input : $string1 = "WillPower" $string2 = "lliW"; Output : No. The above problem can be easily solved in other languages by concatena 3 min read PHP Different characters in the given string Different characters in a given string refer to the unique, non-repeating characters that appear within the string. Identifying these characters involves recognizing each distinct character while excluding any duplicates. This concept is useful in various programming and data analysis tasks where un 2 min read How to check for empty string in PHP ? In this article, we will see how to check for empty string in PHP. String is a set of characters. A string is said to be empty, if it contains no characters. We can use empty() function to check whether a string is empty or not.here we have some common approachesTable of ContentUsing empty() functio 4 min read Like