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 Quote Link to comment 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 Quote Link to comment 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); ?> Quote Link to comment 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 Quote Link to comment 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); ?> Quote Link to comment 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); ?> Quote Link to comment 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) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.