Home  >  Article  >  Backend Development  >  How to remove all uppercase letters from string in php

How to remove all uppercase letters from string in php

青灯夜游
青灯夜游Original
2022-09-26 19:59:033340browse

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

How to remove all uppercase letters from string in php

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(&#39;content-type:text/html;charset=utf-8&#39;);  
function f($str)
{
    $pattern = &#39;/[A-Z]/&#39;;
	$replacement = &#39;&#39;;
    echo preg_replace($pattern, $replacement, $str)."<br>";

}
$str=&#39;Hello World&#39;;
var_dump($str);
f($str);

$str=&#39;abCdEfG&#39;;
var_dump($str);
f($str);

?>

How to remove all uppercase letters from string in php

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(&#39;content-type:text/html;charset=utf-8&#39;);  
function f($str)
{
    $pattern = &#39;/[A-Z]/&#39;;
	$replacement = &#39;&#39;;
    echo "处理后:".preg_filter($pattern, $replacement, $str)."<br><br>";

}
$str=&#39;Hello World&#39;;
echo "原字符串:".$str."<br>";
f($str);

$str=&#39;abCdEfG&#39;;
echo "原字符串:".$str."<br>";
f($str);

?>

How to remove all uppercase letters from string in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn