Check if a String Contains a Special Character in PHP

03-Apr-2023

.

Admin

Check if a String Contains a Special Character in PHP

Hi Dev,

Now, let's see example of Check if a String Contains a Special Character in PHP. We will use PHP Program to Check if a String has a Special Character. We will use How to Check for Special Characters PHP. if you want to see example of How do I Allow Special Characters in PHP? then you are a right place.

There are example to Check if String Contains Check if a String Contains a Special Character in PHP. in this example, we will use to preg_match() function to Check if String Contains Special Character. so Let's the following example with output.

Example 1: Using Special Characters


index.php

<?php

// Check to see if string contains special characters

$string = "One Of $ the Best Site ?is #a ()Nicesnippets.com";

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

{

print("true");

}else{

print ("false");

}

?>

Output:

true

Example 2: Without using special characters

index.php

<?php

// Check to see if string contains special characters

$string = "One Of the Best Site is a Nicesnippets.com";

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

{

print("true");

}else{

print ("false");

}

?>

Output:

false

I hope it could help you...

#PHP