/    Sign up×
Community /Pin to ProfileBookmark

preg_replace help

$today = date(“Ymd”);
$file = example;
$getOldValue = parse_ini_file($file, 1);
if($getOldValue[$today] = ‘ ‘){
$somecontent = $today . “= 0n”;
if (!$handle = fopen($file, ‘a’)) {
echo “Cannot open file ($file)”;
exit;
}
if (fwrite($handle, $somecontent) === FALSE) {
echo “Cannot write to file ($file)”;
exit;
}
fclose($handle);
}
$newValue = $getOldValue[$today] + 1;
$proConf = file_get_contents($file);
$replace = preg_replace(‘($today = ?)’, ‘1$newValue’, $proConf);
file_put_contents($file, $replace);

I want to use this to do:
example:
20060301 = 3
20060302 = 23

to example:
20060301 = 3
20060302 = 24

What I want to print
where $today_from_example = $today{
$today = $oldvalue + 1
}

I have been looking at the preg_replace examples but I don’t understand the syntax that it is using and if the date is not in the file I want to add it.

Thanks for the help

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@JonaMar 01.2006 — [font=trebuchet ms]Can you explain further what you want? I don’t understand what you want to replace (or what you want to replace it with).[/font]
Copy linkTweet thisAlerts:
@BrutusUnixauthorMar 01.2006 — I want to replace:

20060302 = 23

with

20060302 = 24

basicly just a counter,

find todays date and counter value

increment it by 1 and

rewrite the incremented value over the old value
Copy linkTweet thisAlerts:
@JonaMar 01.2006 — [code=php]
function incDate($new, $date){
$date = settype('int');
return $new.($date++);
}

$oldDate = '20060302 = 23';
$newDate = preg_replace('/(d+s=s)(d+)/ie', 'incDate("$1","$2")', $oldDate);

echo "<pre>Old date: $oldDatenNew date: $newDate</pre>";
[/code]
Copy linkTweet thisAlerts:
@BrutusUnixauthorMar 01.2006 — I have it working last problem is with multi lines:

if the file has:

20060301 = 34

20060302 = 3

the file after script will be:

20060302 = 4

I want it to preserve the previous lines

so output should be:

20060301 = 34

20060302 = 4
Copy linkTweet thisAlerts:
@JonaMar 01.2006 — [code=php]
function incDate($a,$b,$c = ''){
if($c){
$c++;
return $a."n".$b.$c;
} else {
return $a.($b++);
}
}

$oldDate = "20060302 = 34n20060301 = 3";
$newDate = preg_replace('/(d+s=sd+)?n?(d+s=s)(d+)/ie', 'incDate("$1","$2","$3")', $oldDate);

echo "<pre>Old date: $oldDatenNew date: $newDate</pre>";
[/code]
×

Success!

Help @BrutusUnix spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...