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
preg_replace() help: trim right space before append value
Moderator: General Moderators
-
jeff00seattle
- Forum Commoner
- Posts: 66
- Joined: Sat Feb 28, 2009 3:27 pm
preg_replace() help: trim right space before append value
Last edited by jeff00seattle on Tue Jun 30, 2009 11:23 am, edited 1 time in total.
-
BornForCode
- Forum Contributor
- Posts: 147
- Joined: Mon Feb 11, 2008 1:56 am
Re: preg_replace() help: trim right space before append value
Code: Select all
$value = trim($value);//remove spaced from the beginning and end
$value = rtrim($value); // from right ltrim is for left,
$value = str_replace(' ','',$value); //this remove all spaces from your script :)
-
jeff00seattle
- Forum Commoner
- Posts: 66
- Joined: Sat Feb 28, 2009 3:27 pm
Re: preg_replace() help: trim right space before append value
Thanks for your reply, but...
I believe I cannot put your solution within the $replacement parameter of function preg_replace().
See: http://us.php.net/preg_replace
Correct me if I am wrong.
Jeff in Seattle
I believe I cannot put your solution within the $replacement parameter of function preg_replace().
See: http://us.php.net/preg_replace
Correct me if I am wrong.
Jeff in Seattle
-
BornForCode
- Forum Contributor
- Posts: 147
- Joined: Mon Feb 11, 2008 1:56 am
Re: preg_replace() help: trim right space before append value
Code: Select all
str_replace(' ','',preg_replace('[0-9]{1,2}\/[0-9]{1,2}\s{1,}',$value,$string));
adding a space after means just
str_replace(' ','',preg_replace('[0-9]{1,2}\/[0-9]{1,2}\s{1,}',$value,$string)).' ';
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: preg_replace() help: trim right space before append value
jeff00seattle wrote: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...
A single preg_replace(...) can also do the trick:
Code: Select all
$text = '4/28 hfdkkdi sielsl fidijg. 5/14 fiisollei sikedi jdsildh 6/3 diel dkisl hgiels dhpwld, 6/13/2009 yeus spwiv wojje rtyu';
$expected = '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';
$mutated = preg_replace('#\d{1,2}/\d{1,2}(?![\d/])#', '$0/2009', $text);
if($mutated === $expected) {
echo 'We did it!';
} else {
echo "Alas, something went wrong...";
}- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: preg_replace() help: trim right space before append value
You're not using any delimiters around your regex pattern, so that can never work.BornForCode wrote:Code: Select all
str_replace(' ','',preg_replace('[0-9]{1,2}\/[0-9]{1,2}\s{1,}',$value,$string)); adding a space after means just str_replace(' ','',preg_replace('[0-9]{1,2}\/[0-9]{1,2}\s{1,}',$value,$string)).' ';
Also, \s{1,} can be shortened to \s+