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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.