Home >Backend Development >PHP Problem >How to use regex to replace Chinese numbers in php

How to use regex to replace Chinese numbers in php

藏色散人
藏色散人Original
2020-08-06 11:03:072772browse

php method to use regular numbers to replace numbers: first create a PHP sample file and set the file encoding; then use "preg_replace($reg, $after, $str);" to implement the function of regular number replacement; Finally execute the file.

How to use regex to replace Chinese numbers in php

Recommended: "PHP Video Tutorial"

<?php
header("Content-type: text/html; charset=utf8");

$str = "我的QQ号是43247943,身高是175厘米,体重是69.5公斤,年龄二十五岁";


echo $str . &#39;<br>';
$reg = '/((0|[1-9]\d*)(\.\d+)?)|(零|一|二|三|四|五|六|七|八|九|十)(百|十|零)?(一|二|三|四|五|六|七|八|九)?(百|十|零)?(一|二|三|四|五|六|七|八|九)?/';
$after = '***';

$newstr = preg_replace($reg, $after, $str);
echo $newstr . '<br>';

Output results


Regular expression matching Chinese numbers

$rule='/(零|一|二|三|四|五|六|七|八|九|十)(百|十|零)?(一|二|三|四|五|六|七|八|九)?(百|十|零)?(一|二|三|四|五|六|七|八|九)?/';

The above is the detailed content of How to use regex to replace Chinese numbers 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