<?php

$string = 'foo';

if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $string))
{
    // one or more of the 'special characters' found in $string
}
Answer from chigley on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-to-check-if-a-string-contains-any-special-character
PHP to Check if a String Contains any Special Character - GeeksforGeeks
July 23, 2025 - Another method to check for special characters in a string is by using the ctype_alnum() function, which checks if all characters in a string are alphanumeric (letters and numbers). the specialChars() function uses the ctype_alnum() function ...
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-check-if-a-string-contains-a-specific-character-in-php
How to check if a String contains a Specific Character in PHP ? - GeeksforGeeks
July 23, 2025 - The string contains the character 'o'. 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.
🌐
Experts PHP
expertsphp.com › php-check-if-string-contains-special-characters
PHP check if string contains special characters - Experts PHP
March 20, 2024 - <?php $string = "Hello! How are you?"; if (preg_match('/[\'^$%&*()}{@#~?><>,|=_+-]/', $string)) { echo "String contains special characters."; } else { echo "String does not contain special characters."; } ?> In this example, the regular expression `/[\'^$%&*()}{@#~?><>,|=_+-]/` checks for the presence of any special characters within the string.
🌐
TutorialsPoint
tutorialspoint.com › article › php-program-to-check-if-a-string-has-a-special-character
PHP program to check if a string has a special character
March 15, 2026 - In PHP, you can check if a string contains special characters using regular expressions with the preg_match() function.
🌐
PrintMyFonts
askingbox.com › tip › php-permit-only-certain-letters-numbers-and-characters-in-a-string
PHP: Permit only certain Letters, Numbers and Characters in a String
August 3, 2026 - These include, for example, points or brackets. If we would like to include such characters in our character class, we have to write this in the following way: if (preg_match("#^[a-zA-Z0-9äöüÄÖÜ \.\]]+$#", $text)) { echo 'String only contains the listed characters.'; }
🌐
PHP Classes
phpclasses.org › package › 12327-PHP-Check-if-a-HTML-string-contains-special-characters.html
IS PHP HTMLSpecialChars: Check if a HTML string contains special characters - PHP Classes
Returns true if the HTML contains special characters, otherwise false. ... require_once('Mind.php'); if($m::aliyilmaz('is_htmlspecialchars')->is_htmlspecialchars($code)){ echo 'HTML contains special characters.'; } else { echo 'HTML does not contain special characters'; }
Find elsewhere
🌐
IncludeHelp
includehelp.com › php › check-if-string-contains-a-particular-character.aspx
Check if string contains a particular character in PHP
If the character wasn’t found at all in the string, strpos() returns false. If its first occurrence is found, it returns true. In the if condition, we simply print to the page that the character was present if it is present and strpos returns true, otherwise, we print the character is not present.
🌐
GitHub
gist.github.com › cferdinandi › 6688744
Test strings for letters, numbers, and special characters. Returns true if they exist, false if they don't. Forked from http://stackoverflow.com/a/9588010/1293256 · GitHub
Test strings for letters, numbers, and special characters. Returns true if they exist, false if they don't. Forked from http://stackoverflow.com/a/9588010/1293256 - has-characters.php
🌐
PHP
php.net › manual › en › function.str-contains.php
PHP: str_contains - Manual
This won't be a concern for most people, but if you are mixing old and new data, especially if reading data from a file, it could be an issue. Here's a "mb_*"-esque function that searches the string: <?php function mb_str_contains(string $haystack, string $needle, $encoding = null) { return $needle === '' || mb_substr_count($haystack, $needle, (empty($encoding) ? mb_internal_encoding() : $encoding)) > 0; } ?> I used mb_substr_count() instead of mb_strpos() because mb_strpos() will still match partial characters as it's doing a binary search.
🌐
PHP Freaks
forums.phpfreaks.com › php coding › regex help
preg_match Check for Special Characters - Regex Help - PHP Freaks
October 25, 2010 - Hi. I'm trying to create a simple function that checks my text strings (ie. firstname & lastnames ect) for illigal characters. I'm using: trim() strip_tags() as well as preg_match I keep getting an echo of True if if I put in valid values. ie. Joe From my understanding the below statement wil...
🌐
PHPHelp
phphelp.com › beginners - learning php
Special Characters ( - & ' " / \ % * # ) validation in a string - Beginners - Learning PHP - PHPHelp
July 6, 2012 - Hello, I am developing a simple form to collect user information. I want a sample PHP code, that will validate the input string to display whether it has any of the below list of special character in the string. Specia…