views:

358

answers:

2

good morning boys and girls...can someone point me to the right direction, please.

i want to replace my php-echo-output »JUNE 29, 2009–JULY 5, 2009« with just plain text: »last week«

<?php
ob_start();
wp_get_archives('type=weekly&limit=1');
$wklyarchives = ob_get_contents();
ob_end_clean();
$wklyarchives = preg_replace('%\&\#8211\;[a-zA-Z0-9, ]*\</a\>%s', 'last week</a>', $wklyarchives);
echo $wklyarchives;
?>

this preg_replace replaces just the 2nd part, so my output is now »JUNE 29, 2009last week« this preg makes me crazy...

+1  A: 

You are only matching the dash and the part after it, so that's exactly what is getting replaced. If you add the same character class before the dash, like

[a-zA-Z0-9, ]%\&\#8211\;[a-zA-Z0-9, ]

it should work (depending on what $wklyarchives contains even before the 'JUNE 29' part; you might have to make sure you don't mach too much).

balpha
$wklyarchives = preg_replace('[a-zA-Z0-9, ]%\[a-zA-Z0-9, ]*\</a\>%s', 'last week</a>', $wklyarchives);thanx for your answer...i dont know why, but it don't work.
toul
What is the *exact* content of $wklyarchives before the replacing?
balpha
i got this output:JUNE 29, 2009–JULY 5, 2009
toul
The dash between 2009 and JULY: Is that a normal dash, an em dash, an en dash, an escaped one? The one you're posting is a regular dash, but from your regex it seems it should be an escaped en dash.
balpha
it's a normal one, i think
toul
Then why is this: %\ in your regex?
balpha
A: 

Okay, tried this with WordPress, worked fine:

'/[a-z0-9,]+ [0-9,]+ [0-9]+.?[a-z0-9,]+ [0-9,]+ [0-9]+/iu'

Remember, do not use output buffering with wp_get_archives. Use echo=0:

$wklyarchives = wp_get_archives("type=weekly&limit=1&echo=0");

Good luck.

kovshenin