How do you check if a string contains any special character in php?
Hi all, I want to check if these characters exist in a string by using
Help please!
SharpC 6,3584 gold badges42 silver badges39 bronze badges asked Oct 14, 2010 at 22:12
4
answered Oct 14, 2010 at 22:20
chigleychigley 2,49219 silver badges18 bronze badges 3
answered Oct 15, 2010 at 1:47
PetahPetah 44.6k27 gold badges155 silver badges211 bronze badges 1 Not the answer you're looking for? Browse other questions tagged php or ask your own question.To check if a string has a special character, the PHP code is as follows; ExampleLive Demo ?/|}{~:]', $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); ?> OutputString 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"); }
Updated on 17-Aug-2020 11:31:47
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
How do I check if a string contains special characters?To check if a string contains special characters, call the test() method on a regular expression that matches any special character. The test method will return true if the string contains at least 1 special character and false otherwise.
How do I allow special characters in PHP?Tip: To convert special HTML entities back to characters, use the htmlspecialchars_decode() function.. & (ampersand) becomes &. " (double quote) becomes ". ' (single quote) becomes '. < (less than) becomes <. > (greater than) becomes >. How check string is value or not in PHP?The is_string() function checks whether a variable is of type string or not. This function returns true (1) if the variable is of type string, otherwise it returns false/nothing.
How do you check if a string contains a number in PHP?The is_numeric() function checks whether a variable is a number or a numeric string. This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.
|