2

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');
3

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 functions) at Pastebin.

  • Thanks @Sally - that does replace the Roman numbers, but it removes the link all together...any idea why? – Nathan Mar 6 '18 at 4:08
  • Ah, actually it does work - it was just the Pastebin code that was not working. Thanks! – Nathan Mar 6 '18 at 4:09
  • 1
    Sorry @Nathan, I forgot to wrap the replaced text in <a> and </a>. But I've fixed it, including the one at Pastebin. =) – Sally CJ Mar 6 '18 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.