PHP preg_match – only allow alphanumeric strings and – _ characters

Today, We want to share with you PHP preg_match – only allow alphanumeric strings and – _ characters.In this post we will show you php check if string contains special characters, hear for php check if string contains special characters we will give you demo and example for implement.In this post, we will learn about Remove all characters except letters and numbers in PHP with an example.

PHP preg_match – only allow alphanumeric strings and – _ characters

There are the Following The simple About PHP preg_match – only allow alphanumeric strings and – _ characters Full Information With Example and source code(test if string is alphanumeric php).

As I will cover this Post with live Working example to develop php remove non alphanumeric characters, so the php preg_replace only allow alphanumeric for this example is following below.

PHP preg_match Explanation

  • [] => character class definition
  • ^ => negate the class
  • a-z => chars from ‘a’ to ‘z’
  • _ => underscore
  • – => hyphen ‘-‘ (We need to escape it)
  • 0-9 => numbers (from zero to nine)
  • Note: The use of the ‘i’ modifier at the end of simple regex is for check rules like as a ‘case-insensitive’ if We don’t put that We shall should to include the upper case characters in the PHP source code before by doing A-Z
if(preg_match('/[^a-z_\-0-9]/i', $string))
{
  echo "not valid string";
}

index.php





 
 pakainfo.com - Employee Information Sample HTML Form
 



  " .$errorMsg. "

" ;} ?>
Employee Name: >

full example : php preg_match case insensitive

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP preg_match – only allow alphanumeric strings and – _ characters.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment