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.

  Vue.js CRUD Example Tutorial For Beginners

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

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.


Download Project Scripts

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 :

  How to Check If String Contains a Substring in PHP

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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Laravel 6 Sweet Alert Tutorial With Example
  2.   Angular 9/8 Material Radio Button Examples
  3.   PHP GET HTML source code WebPages using DOM Parser
  4.   Upload Multiple Images Preview using PHP
  5.   Generate PDF Files using Laravel 5.7 Dynamically Scripts
  6.   Vuejs autocomplete search with typeahead js
  7.   PHP MySQL Inline Editing Dynamically using jQuery Ajax
  8.   Convert object to Array in PHP Example
  9.   Laravel Nested Relationships in Eloquent Example
  10.   PHP GET HTML source code WebPages using DOM Parser

Leave a Reply

Your email address will not be published. Required fields are marked *