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 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?