dousi6087 2015-07-11 18:44
浏览 60
已采纳

Str_replace替代PHP中的后续字符串

In my script I replaced all "," commas with quotation+spaces.

But when it comes to numbers which are like 3,456,778, it also converts the commas to quote+space. Is there any way to add to command to ignore big numbers like that so it doesn't convert it to:

3" 456" 778"

If there is quotationm+space+any number then convert quotation+space to comma.. I mean i know how to do it with str_replace command but i dont know how to select anynumber 0-9. Any help to do it? To convert it to:

3,456,778

I think i need to elaborate some. I needed convert this text:

Value=3,456,778,id=777

To:

Value=3,456,778" id=777"

But problem is it also convert those middle commas in between numbers. So even if I can change my str_replace command to this like "If comma is not in between two numbers then only convert comma to quotation+space". It would be good. Is it possible?

  • 写回答

3条回答 默认 最新

  • duadlkc5762218 2015-07-11 18:55
    关注

    What about this?

    preg_replace("/,([^0-9]|$)/", "\"$1", $text);
    

    This will match all the text except commas followed by numbers.

    For instance, this:

    $text = "123,23 adas , asdsa d, asdasd sa 1234,234324,asdas 324324 234,";
    echo $text; echo "<br/>";
    echo preg_replace("/,([^0-9]|$)/", "\"$1", $text);
    

    Will echo this:

    123,23 adas , asdsa d, asdasd sa 1234,234324,asdas 324324 234"
    123,23 adas " asdsa d" asdasd sa 1234,234324"asdas 324324 234"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件