Regex usage for remove special characters in PHP
Categories: PHP
Pushing data into MySQL using PHP regex ✏️
1. preg_replace()
🔔 Searches subject for matches to pattern and replaces them with replacement.
preg_replace() is a implemented function in PHP. Syntax is the following:
<?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
?>
2. Syntax
🔔 Various types can be made by using Regex
Remove all special characters
$string = preg_replace(" /[#&+%@=/;.'"^`|!$<>())[]{}]/i", "", $string);
$string = addslashes($string);
References
PHP Official Document
Penggu's blog
Celestia's blog
🌜 This is my personal study blog. If you find any errors or mistakes,
please feel free to point them out in the comments or via email. 💌
I would greatly appreciate it! 😄✨💛
Leave a comment