简体   繁体   中英

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. 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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