preg_replace() help: trim right space before append value
Posted: Tue Jun 30, 2009 11:12 am
Hi
I having beating my head over the following, I want replacement to trim all right spaces from match before replace and then append a value with a single space, using preg_replace().
For example, I have a text that is filled with dates with Month/Day and Month/Day/Year. I want to replace all Month/Day(s) and append with Year. To elaborate:
Match 6/13 => Replace 6/13/2009
6/13/2009 => Do nothing
So far, the following Match regex works: '[0-9]{1,2}\/[0-9]{1,2}\s{1,}'
4/28 hfdkkdi sielsl fidijg. 5/14 fiisollei sikedi jdsildh 6/3 diel dkisl hgiels dhpwld, 6/13/2009 yeus spwiv wojje rtyu
But this replace '\0/2009 ' does the following:
4/28 /2009 hfdkkdi sielsl fidijg. 5/14 /2009 fiisollei sikedi jdsildh 6/3 /2009 diel dkisl hgiels dhpwld, 6/13/2009 yeus spwiv wojje rtyu
Notice that it does not replace the space(s) after the Month/Day
So, how can I create a $replacement parameter of preg_replace() that removes all spaces after the match and then append value with a single space so it would look like this?:
4/28/2009 hfdkkdi sielsl fidijg. 5/14/2009 fiisollei sikedi jdsildh 6/3/2009 diel dkisl hgiels dhpwld, 6/13/2009 yeus spwiv wojje rtyu
Thanks
Jeff in Seattle
I having beating my head over the following, I want replacement to trim all right spaces from match before replace and then append a value with a single space, using preg_replace().
For example, I have a text that is filled with dates with Month/Day and Month/Day/Year. I want to replace all Month/Day(s) and append with Year. To elaborate:
Match 6/13 => Replace 6/13/2009
6/13/2009 => Do nothing
So far, the following Match regex works: '[0-9]{1,2}\/[0-9]{1,2}\s{1,}'
4/28 hfdkkdi sielsl fidijg. 5/14 fiisollei sikedi jdsildh 6/3 diel dkisl hgiels dhpwld, 6/13/2009 yeus spwiv wojje rtyu
But this replace '\0/2009 ' does the following:
4/28 /2009 hfdkkdi sielsl fidijg. 5/14 /2009 fiisollei sikedi jdsildh 6/3 /2009 diel dkisl hgiels dhpwld, 6/13/2009 yeus spwiv wojje rtyu
Notice that it does not replace the space(s) after the Month/Day
So, how can I create a $replacement parameter of preg_replace() that removes all spaces after the match and then append value with a single space so it would look like this?:
4/28/2009 hfdkkdi sielsl fidijg. 5/14/2009 fiisollei sikedi jdsildh 6/3/2009 diel dkisl hgiels dhpwld, 6/13/2009 yeus spwiv wojje rtyu
Thanks
Jeff in Seattle