How do you check if a string contains any special character in php?

Possible Duplicate:
preg_match php special characters

Hi all, I want to check if these characters exist in a string by using preg_match:

^'£$%^&*()}{@'#~?><>,@|\-=-_+-¬'

Help please!

How do you check if a string contains any special character in php?

SharpC

6,3584 gold badges42 silver badges39 bronze badges

asked Oct 14, 2010 at 22:12

4

<>,|=_+¬-]/', $string))
{
    // one or more of the 'special characters' found in $string
}

answered Oct 14, 2010 at 22:20

chigleychigley

2,49219 silver badges18 bronze badges

3

preg_match('/'.preg_quote('^\'£$%^&*()}{@#~?><,@|-=-_+-¬', '/').'/', $string);

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;

Example

 Live 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);
?>

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");
}

How do you check if a string contains any special character in php?

Updated on 17-Aug-2020 11:31:47

  • Related Questions & Answers
  • 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
  • C++ Program to check if a string has been seen before
  • Python program to check if a string contains any unique character
  • C# Program to check if a character is a whitespace character
  • Check if a string has white space in JavaScript?
  • How to Check if PHP session has already started?
  • Check if string contains special characters in Swift
  • Check if a string can be rearranged to form special palindrome in Python
  • How to check if a string has a certain piece of text in JavaScript?
  • C# program to determine if a string has all unique characters
  • How to check if a character in a string is a letter in Python?

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

// Check to see if string contains special characters (excluding dash)
if(preg_match('/[^a-zA-Z0-9-]+/', $string, $matches)):
// Contains special character
else:
// String is clean
endif;
?>

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.