Jump to content

preg_replace question


Murciano

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/203283-preg_replace-question/
Share on other sites

@<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.

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.