Jump to content

add dots in digits


mraza

Recommended Posts

Hi,

 

I have a text where i need to replace space between digits with dots.

example:

And today date 2011 03 28 and also some other text
and this could be second line of7 2 6 0 and continue like this...
some other number like 8 0 6 9 and some other as well.
but it will not add dot to this single digit 01 or even to this word 2000 as those are single 
// there could be more this type of digits 

 

Now i need to replace that date to 2011.03.28 and of7.2.6.0  and 8.0.6.9, i have various this type of digits i need to replace only space with dots between digits using regex, any help please to show me a regex pattern plz.

Thanks

Link to comment
https://forums.phpfreaks.com/topic/232326-add-dots-in-digits/
Share on other sites

<?php
$test = 'And today date 2011 03 28 and also some other text
and this could be second line of7 2 6 0 and continue like this...
some other number like 8 0 6 9 and some other as well.
but it will not add dot to this single digit 01 or even to this word 2000 as those are single 
// there could be more this type of digits ';
function my_to_dot($a){
    return preg_replace('/\s+/', '.', $a[0]);
}
echo preg_replace_callback('/(\d+\s+){2,}/', 'my_to_dot', $test);
?>

Link to comment
https://forums.phpfreaks.com/topic/232326-add-dots-in-digits/#findComment-1195799
Share on other sites

Thanks a lot for the code, i have actually a little problem still in that. if i use that code or mine above it will add dots to numbers i wants but i do not wants it to add dot at the last number,

 

like its converting this 2011 03 02 to 2011.03.02.  <<< it is adding one dot at the end, is it possible to convert that to 2011.03.02 so no dot at the end.

 

Thanks again

Link to comment
https://forums.phpfreaks.com/topic/232326-add-dots-in-digits/#findComment-1195855
Share on other sites

<?php
$str = "And today date 2011 03 28 and also some other text
and this could be second line of7 2 6 0 and continue like this...
some other number like 8 0 6 9 and some other as well.
but it will not add dot to this single digit 01 or even to this word 2000 as those are single
// there could be more this type of digits";
echo preg_replace("/(\d)(\s+)(\d)/", '$1.$3', $str);
?>

Link to comment
https://forums.phpfreaks.com/topic/232326-add-dots-in-digits/#findComment-1195889
Share on other sites

<?php
$test = 'And today date 2011 03 28 and also some other text
and this could be second line of7 2 6 0 and continue like this...
some other number like 8 0 6 9 and some other as well.
but it will not add dot to this single digit 01 or even to this word 2000 as those are single 
// there could be more this type of digits ';
function my_to_dot($a){
    return trim(preg_replace('/\s+/', '.', $a[0]),'.'). ' ';
}
echo
preg_replace_callback('/(\d+\s+){2,}/', 'my_to_dot', $test);

?>

Link to comment
https://forums.phpfreaks.com/topic/232326-add-dots-in-digits/#findComment-1195960
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.