- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP program to check if a string has a special character
To check if a string has a special character, the PHP code is as follows;
Example
<?php function check_string($my_string){ $regex = preg_match('[@_!#$%^&*()<>?/|}{~:]', $my_string); if($regex) print("String has been accepted"); else print("String has not been accepted"); } $my_string = 'This_is_$_sample!'; check_string($my_string); ?>
Output
String has not been accepted
Above, a function named ‘check_string’ is defined, that takes a string as its parameter −
$my_string = 'This_is_$_sample!';
Use regular expression to check if a string has a special character. If there is a special character, a specific message is printed. Outside the function, the string is defined, and the function is called by passing the string as parameter −
function check_string($my_string){ $regex = preg_match('[@_!#$%^&*()<>?/|}{~:]', $my_string); if($regex) print("String has been accepted"); else print("String has not been accepted"); }
- Related Articles
- C# program to check if a string contains any special character
- Java program to check if a string contains any special character
- Program to check if a string contains any special character in C
- Program to check if a string contains any special character in Python
- C# Program to replace a special character from a String
- Python program to check if a string contains any unique character
- C++ Program to check if a string has been seen before
- C# Program to check if a character is a whitespace character
- Check if a string can be rearranged to form special palindrome in Python
- Check if string contains special characters in Swift
- MySQL query to display a substring before a special character in a string
- How to remove partial string after a special character in R?
- How to check if a character in a string is a letter in Python?
- Check if a string has white space in JavaScript?
- PHP 8 – Using str_contains() to check if a string contains a substring

Advertisements