Home > Article > Backend Development > How to remove all uppercase letters from string in php
Two removal methods: 1. Use preg_replace() to execute a regular expression to search for all uppercase letters and replace them with empty characters. The syntax is "preg_replace('/[A-Z]/','', $str)". 2. Use preg_filter() to execute a regular expression to search for all uppercase letters and replace them with empty characters. The syntax is "preg_filter('/[A-Z]/','',$str)".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In php, you can use preg_replace() or preg_filter() works with regular expressions to remove all uppercase letters in a string.
Regular expression used:
/[A-Z]/
Function: Search for all uppercase letters
Method 1: Use preg_replace() for regular replacement
preg_replace() function can perform regular expression search and replacement
Just use preg_replace( ) just execute a regular expression to search for all uppercase letters and replace them with null characters.
<?php header('content-type:text/html;charset=utf-8'); function f($str) { $pattern = '/[A-Z]/'; $replacement = ''; echo preg_replace($pattern, $replacement, $str)."<br>"; } $str='Hello World'; var_dump($str); f($str); $str='abCdEfG'; var_dump($str); f($str); ?>
Method 2: Use preg_filter() for regular replacement
preg_filter() and preg_replace( ) function, you can perform regular expression search and replace.
Just perform a regex to search for all uppercase letters and replace them with null characters.
<?php header('content-type:text/html;charset=utf-8'); function f($str) { $pattern = '/[A-Z]/'; $replacement = ''; echo "处理后:".preg_filter($pattern, $replacement, $str)."<br><br>"; } $str='Hello World'; echo "原字符串:".$str."<br>"; f($str); $str='abCdEfG'; echo "原字符串:".$str."<br>"; f($str); ?>
Explanation: The difference between preg_replace() and preg_filter()
preg_filter() function only returns the result of successful matching. And preg_replace() returns all results, regardless of whether the match is successful.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove all uppercase letters from string in php. For more information, please follow other related articles on the PHP Chinese website!