I’m trying to replace the Roman numbers in the Archive Widget link (and all other numbers) with Khmer (Cambodian) numbers.
But while I do replace the link text with this code, it also replaces the link url which then breaks the link.
How do I just replace the archive link text, and not the url?
function convert_numbers_to_khmer( $string ) {
$khmer_numbers = array('០', '១', '២', '៣', '៤', '៥', '៦', '៧', '៨', '៩', '.');
$english_numbers = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.');
return str_replace($english_numbers, $khmer_numbers, $string);
}
function make_khmer_time( $the_time ) {
if ( get_bloginfo( 'language' ) == 'km' ) {
$the_time = convert_numbers_to_khmer( $the_time );
}
return $the_time;
}
add_filter( 'get_the_time', 'make_khmer_time' );
add_filter( 'get_the_date', 'make_khmer_time' );
add_filter('comments_number', 'make_khmer_time');
add_filter('get_archives_link', 'make_khmer_time');
1 Answer
You can use regular expression and the preg_replace_callback()
function:
function _make_khmer_link_replace_callback( array $matches ) {
return '<a' . $matches[1] . '>' . convert_numbers_to_khmer( $matches[2] ) . '</a>';
}
function make_khmer_link( $link ) {
if ( get_bloginfo( 'language' ) == 'km' ) {
$link = preg_replace_callback( '#<a(.*?)>(.+?)</a>#', '_make_khmer_link_replace_callback', $link );
}
return $link;
}
add_filter('get_archives_link', 'make_khmer_link');
PS: You can see the full code (including your existing function
s) at Pastebin.