替换php中的完全匹配 [英] replace exact match in php

查看:33
本文介绍了替换php中的完全匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触 php 中的正则表达式.

im new to regular expressions in php.

我有一些数据,其中一些值存储为零(0).我想要做的是用-"替换它们.我不知道哪个值会随着我的数据库表每天更新而变为零,这就是为什么我必须在所有数据上放置替换内容.

I have some data in which some of the values are stored as zero(0).What i want to do is to replace them with '-'. I dont know which value will get zero as my database table gets updated daily thats why i have to place that replace thing on all the data.

$r_val=preg_replace('/(0)/','-',$r_val);

我使用的代码正在替换它找到的所有零,例如.它甚至从 104.67 替换零,给出错误的输出 1-4.56.我想要那些值精确为零的数据,必须用-"替换它,而不是它遇到的每个零.任何人都可以请帮忙!!

The code im using is replacing all the zeroes that it finds for eg. it is even replacing zero from 104.67,giving the output 1-4.56 which is wrong. i want that data where value is exact zero that must be replaced by '-' not every zero that it encounter. Can anyone please help!!

$r_val 具有的值的示例:-

Example of the values that $r_val is having :-

10.31,391.05,113393,15.31,1000 等

10.31, 391.05, 113393, 15.31, 1000 etc.

推荐答案

这在很大程度上取决于您的数据在 $r_val 中的格式,但一个好的起点是尝试:

This depends alot on how your data is formatted inside $r_val, but a good place to start would be to try:

$r_val = preg_replace('/(?<!\.)\b0\b(?!\.)/', '-', $r_val);

其中 \b 是一个长度为 0 的字符,表示单词"的开头或结尾.

Where \b is a 0-length character representing the start or end of a 'word'.

听起来很奇怪,但 Perl regex 文档 实际上非常适合解释preg_* 函数的正则表达式部分,因为 Perl 是实际实现功能的地方.

Strange as it may sound, but the Perl regex documentation is actually really good for explaining the regex part of the preg_* functions, since Perl is where the functionality is actually implemented.

这篇关于替换php中的完全匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆