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).

Also Read :  PHP Laravel 5.7 insert update delete in MySQL Example

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
Also Read :  PHP MySQLi Database Connection

if(preg_match('/[^a-z_\-0-9]/i', $string))
{
  echo "not valid string";
}

index.php

<?php
if(isset($_POST['Submit'])){

  $emp_name=trim($_POST["emp_name"]);

  if(preg_match('/[^a-z_\-0-9]/i', $emp_name)){
    $errorMsg=  "error : You did not enter a valid name.";
    $code= "1" ;
  }
  else{
      echo "Success";
      //final source code will run/execute here.
    }

}
?>
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>pakainfo.com - Employee Information Sample HTML Form</title>
 <style type="text/css" >
  .errorMsg{border:1px solid red; }
  .message{color: red; font-weight:bold; }
 </style>
</head>

<body>
  <?php if (isset($errorMsg)) { echo "<p class='message'>" .$errorMsg. "</p>" ;} ?>

<form name= "registration" id= "registration" method= "post" action= "">
<table width= "400" border= "0" align="center" cellpadding= "4" cellspacing= "1">
<tr>
<td>Employee Name:</td>
<td><input name= "emp_name" type= "text" id="emp_name" value="<?php if(isset($name)){echo $name;} ?>"
<?php if(isset($code) && $code == 1){echo "class=errorMsg" ;} ?> ></td>
</tr>
<tr>
<td></td>
<td><input type= "submit" name= "Submit" value= "Submit"></td>
</tr>
</table>
</form>
<p>full example : php preg_match case insensitive</p>
</body>
</html>

Angular 6 CRUD Operations Application Tutorials

Read :

Also Read :  Angularjs Login And Registration Modal Template

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 Reply

avatar
  Subscribe  
Notify of