Home >Backend Development >PHP Tutorial >Usage analysis of preg_replace regular replacement in php [replace multiple values ​​at one time]

Usage analysis of preg_replace regular replacement in php [replace multiple values ​​at one time]

高洛峰
高洛峰Original
2017-01-20 09:49:242078browse

The example in this article describes the usage of preg_replace regular replacement in php. Share it with everyone for your reference, the details are as follows:

1.php’s preg_replace and str_replace are both default /g, replace them all

2. If you need to use regular expressions, you need to use preg_replace

<?php
$a = "abc defa
bcd ef";
$b= preg_replace("/\t|a/","",$a);
echo($b);
/*
输出:
bc def
bcd ef
*/
?>

In addition, comparing replace in js, I feel that the syntax of php is not beautiful

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
var a="a b";
console.log(a.replace(/\t/g,""));
</script>
</body>
</html>

The running effect diagram is as follows:

Usage analysis of preg_replace regular replacement in php [replace multiple values ​​at one time]

I hope this article will help The above will be helpful to everyone in PHP programming.

For more analysis on the usage of preg_replace regular replacement in PHP [replacing multiple values ​​at one time], please pay attention to the PHP Chinese website for related articles!

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