powershell - Searching for a specific string of text, then replacing the string -
i have php file content similar this:
<table class="select_descript" cellspacing="4" cellpadding="0" border="0" align="center" valign="middle"><tbody><tr><td>top songs past week. updated saturday, 21 march 2015</td></tr></tbody></table>
i have date in there, want able replace date section in php file current date. i'm thinking of making use of finding both valign="middle"><tbody><tr><td>
, word updated
, use -replace method.
$phpfile = phpfile.php (get-content $phpfile) -replace "(?<=updated ).+","updated (get-date).tolongdatestring()"
but stuck expression.
you don't under circumstances file being changed. first suggestion create source template file unique token current date goes.
...<td>top songs past week. updated [[date_here]]</td>...
then replacement straightforward , foolproof.
(gc template.php) -replace '[[date_here]]',(get-date).tolongdatestring() | sc $phpfile
for regex solution, need string identifies location least change. wouldn't try parse based on html tags, or change in formatting break regex. in example use whole "top songs ..." text.
$searchtext = 'top songs past week. updated [^<]*' $replacetext = "top songs past week. updated $((get-date).tolongdatestring())" (get-content $phpfile -raw) -replace $searchtext,$replacetext | set-content $phpfile
note -raw
parameter get-content
causes read whole file @ once , close file don't error when pass same filename set-content.
Comments
Post a Comment