Failing_Solutions Posted June 3, 2012 Share Posted June 3, 2012 Hi folks, I'm trying to use php to replace Number-Space-Number with Number-Comma-Number Example 7 1036551 Would be 7,1036551 I've tried $numbers='/([0-9]) ([0-9])/'; $replacement =','; $str=preg_replace($numbers,$replacement,$str); But no luck.. Any help is appreciated Thank you Quote Link to comment https://forums.phpfreaks.com/topic/263579-preg_replace-any-number-space-any-number/ Share on other sites More sharing options...
requinix Posted June 3, 2012 Share Posted June 3, 2012 That would be fine but you aren't adding back in the two digits that the regex matched. $replacement = '$1,$2'; Or you could use lookarounds but I personally avoid them when possible. Quote Link to comment https://forums.phpfreaks.com/topic/263579-preg_replace-any-number-space-any-number/#findComment-1350805 Share on other sites More sharing options...
Failing_Solutions Posted June 3, 2012 Author Share Posted June 3, 2012 Thanks for the reply requinix but still not getting the results $numbers='/([0-9]) ([0-9])/'; $replacement ='$1,$2'; $str=preg_replace($numbers,$replacement,$str); My results are still showing Array ( [0] => 1036550 [1] => Chronomancer [2] => 1 [3] => Very Long [4] => Crucias [5] => 7 1036551 [6] => Chronomancer [7] => 1 [8] => Very Long [9] => Crucias [10] => 7 1036553) Think something is wrong with my $numbers variable.. Just trying to add that comma in there so I can explode it correctly on commas. Quote Link to comment https://forums.phpfreaks.com/topic/263579-preg_replace-any-number-space-any-number/#findComment-1350808 Share on other sites More sharing options...
requinix Posted June 3, 2012 Share Posted June 3, 2012 Are you sure it's really a space and not a tab or newline character? Quote Link to comment https://forums.phpfreaks.com/topic/263579-preg_replace-any-number-space-any-number/#findComment-1350809 Share on other sites More sharing options...
Failing_Solutions Posted June 3, 2012 Author Share Posted June 3, 2012 humm good point didn't think of that $numbers='/([0-9])\s+([0-9])/'; Fixed it, thank you very much. Also I did have a question on how / why $1, $2 works in the replacements. Are those some sort of automatically derived variables? If looking for like 3 numbers 121 222 333 would the third number replaced automatically be $3? Just want to understand it, Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/263579-preg_replace-any-number-space-any-number/#findComment-1350814 Share on other sites More sharing options...
requinix Posted June 3, 2012 Share Posted June 3, 2012 Not sure what you mean, but $number comes from the matched groups in the search expression - the ones you put ()s around. If you had /([0-9])\s+([0-9])([0-9])/ (which is inefficient but suitable for this example) then $1=the digit before the space, $2=the digit immediately after, and $3=the digit immediately after that. \1, \2, and \3 also work. If you wanted groups of numbers then you'd have to change the expression a little: it only matches individual digits right now. /(\d+)\s+(\d+)\s+(\d+)/ with \d being shorthand for [0-9] and + meaning "at least one of", then $1-3 are the first through third numbers. Then you'd replace it with $1,$2,$3 (or \1,\2,\3) Quote Link to comment https://forums.phpfreaks.com/topic/263579-preg_replace-any-number-space-any-number/#findComment-1350821 Share on other sites More sharing options...
Failing_Solutions Posted June 3, 2012 Author Share Posted June 3, 2012 Thank you for the great explanation. Very helpful! Quote Link to comment https://forums.phpfreaks.com/topic/263579-preg_replace-any-number-space-any-number/#findComment-1350920 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.