Murciano Posted May 29, 2010 Share Posted May 29, 2010 Im trying to get a preg_replace to work for editing dates in an txt file, all the dates are different and there are hundreds of them that have to be changed, i been reading up about preg_replace and came up with this line of code <?php $body = preg_replace("@<tDate>[0-9]/{4,5}</tDate>@", "<tDate>NEW DATE GOES HERE</tDate>", $body); ?> Its running ok (no errors,) but nothing is being updated in the txt file am i on the right path or going way off course with this line Quote Link to comment Share on other sites More sharing options...
ZachMEdwards Posted May 29, 2010 Share Posted May 29, 2010 @<tDate>[0-9]/{4,5}</tDate>@ Match the characters “@<tDate>” literally «@<tDate>» Match a single character in the range between “0” and “9” «[0-9]» Match the character “/” literally «/{4,5}» Between 4 and 5 times, as many times as possible, giving back as needed (greedy) «{4,5}» Match the characters “</tDate>@” literally «</tDate>@» It should work, depends on what your $body looks like. Quote Link to comment Share on other sites More sharing options...
Murciano Posted May 29, 2010 Author Share Posted May 29, 2010 Thanks for the quick reply Zach, so i am on the right track, will have to look into things more deeply, it doesnt work however so what could be the problem with my $body, im calling the preg_replace right after this $body = file_get_contents('datafeed.txt', $body); could it be that?.. a couple of lines further down from my preg_replace i have a str_replace which works ok could there be some kind of clash? Quote Link to comment Share on other sites More sharing options...
ZachMEdwards Posted May 29, 2010 Share Posted May 29, 2010 Maybe you could post a small snippet of $body? You should be OK if $body looks like: @<tDate>5/////</tDate>@ @<tDate>9////</tDate>@ Quote Link to comment Share on other sites More sharing options...
.josh Posted May 30, 2010 Share Posted May 30, 2010 post what your current date format looks like (examples of actual content) and what you want it to be changed to. We can't tell you what's wrong with your pattern if we don't know what it's supposed to be looking for. 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.