mraza Posted March 31, 2011 Share Posted March 31, 2011 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 More sharing options...
mraza Posted March 31, 2011 Author Share Posted March 31, 2011 $change = preg_replace("/(\d+)(\s)|(\s)(\d+)/", "$1.$3", $change); fixed Link to comment https://forums.phpfreaks.com/topic/232326-add-dots-in-digits/#findComment-1195231 Share on other sites More sharing options...
sasa Posted April 2, 2011 Share Posted April 2, 2011 <?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 More sharing options...
mraza Posted April 2, 2011 Author Share Posted April 2, 2011 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 More sharing options...
The Little Guy Posted April 2, 2011 Share Posted April 2, 2011 <?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 More sharing options...
sasa Posted April 2, 2011 Share Posted April 2, 2011 <?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 More sharing options...
salathe Posted April 4, 2011 Share Posted April 4, 2011 You can use a plain regex without any fancy callback shenanigans. Here's one that's as simple as I can make it. preg_replace('/(\G|\d)\s(\d)/', '$1.$2', $subject) Link to comment https://forums.phpfreaks.com/topic/232326-add-dots-in-digits/#findComment-1196552 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.