简体   繁体   English

在PHP中跟随字符串的Str_replace替代方法

[英]Str_replace alternative for following strings in 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. 但是,当涉及到3,456,778类的3,456,778 ,它也会将逗号转换为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 . 如果有quotationm + space +任何数字,然后将quotation + space转换为逗号。。我的意思是我知道如何使用str_replace命令来执行此操作,但是我不知道如何选择任何数字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". 因此,即使我可以将str_replace命令更改为“如果逗号不在两个数字之间,则只能将逗号转换为引号+空格”。 It would be good. 这将是很好的。 Is it possible? 可能吗?

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"

It is not really clear from your description what you actually want to do. 从您的描述中并不清楚您实际上想要做什么。

This might be a step into the right direction, however: 但是,这可能是朝正确方向迈出的一步:

preg_replace('/([0-9]+)" /', '\\1,', '3" 456" 778"');

Not the best solution maybe,but can give it a try. 也许不是最好的解决方案,但是可以尝试一下。

$copy_date = '3" 456" 778"';
$copy_date = preg_replace("(\"\s{1})", ",", $copy_date);

$copy_date1 = preg_replace("(\")", "", $copy_date);
print $copy_date1;

o/p:3,456,778

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM