How to use regex to replace Chinese numbers in php

藏色散人
Release: 2023-03-03 21:48:02
Original
2726 people have browsed it

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>';
Copy after login

Output results


Regular expression matching Chinese numbers

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

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!