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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.