首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >替换多个字符串或文本

替换多个字符串或文本
EN

Stack Overflow用户
提问于 2021-02-16 08:31:59
回答 3查看 187关注 0票数 1

如果您必须在任何页面中替换或翻译functions.php中的一个字符串或文本,那么这段代码在WordPress中工作得很好:

代码语言:javascript
复制
//The filters.. both are required.
add_filter('gettext', 'change_cancellation_btn');
add_filter('ngettext', 'change_cancellation_btn');

//function
function change_cancellation_btn($cancellation_btn){
$cancellation_btn = str_ireplace('text to replace', 'new text', $cancellation_btn);
return $cancellation_btn;
}

如何使用相同的代码替换多个字符串或文本?

在我的例子中,我用两个不同的按钮替换了文本,为了做到这一点,我使用了两次相同的代码:

代码语言:javascript
复制
//The filters.. both are required.
add_filter('gettext', 'change_cancellation_btn');
add_filter('ngettext', 'change_cancellation_btn');

//function button 1
function change_cancellation_btn($cancellation_btn){
$cancellation_btn = str_ireplace('text 1 to replace', 'new text 1', $cancellation_btn);
return $cancellation_btn;
}


//The filters.. both are required.
add_filter('gettext', 'change_nocancellation_btn');
add_filter('ngettext', 'change_nocancellation_btn');

//function button 2
function change_nocancellation_btn($nocancellation_btn){
$nocancellation_btn = str_ireplace('text 2 to replace', 'new text 2', $nocancellation_btn);
return $nocancellation_btn;
}

那么,有办法用一个函数替换多个字符串吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-02-16 08:41:20

您可以使用如下所示的开关情况:

代码语言:javascript
复制
add_filter( 'gettext', 'theme_change_comment_field_names', 20, 3 );
function theme_change_comment_field_names( $translated_text, $text, $domain ) {
  
    switch ( $translated_text ) {
 
        case 'text 1 to replace' :
 
            $translated_text = __( 'new text 1', 'theme_text_domain' );
            break;
 
        case 'text 2 to replace' :
 
            $translated_text = __( 'new text 2', 'theme_text_domain' );
            break;
    }    
 
    return $translated_text;
}

还没测试过。

票数 0
EN

Stack Overflow用户

发布于 2021-02-16 09:09:06

代码语言:javascript
复制
Call the function inside the loop
and pass the parameter by creating an array or if you from a database similar its work.

e.g:
$array = array(
array(
'text_to_replace' => 'Dummy',
'new_text' => 'Test',
'text_content'=> 'Dummy content string'
))

//function
function change_cancellation_btn($text_to_replace, $new_text, $text_content){
$changedText = str_ireplace($text_to_replace, $new_text, $text_content);
return $changedText;
}


Now call the change_cancellation_btn by running the loop of the array by passing multiple contents.
票数 1
EN

Stack Overflow用户

发布于 2021-02-24 11:24:09

最新消息。这对我来说更好,因为它也替换/翻译了管理后端中的所有字符串:

代码语言:javascript
复制
add_filter(  'gettext',  'dirty_translate'  );
add_filter(  'ngettext',  'dirty_translate'  );
function dirty_translate( $translated ) {
     $words = array(
            // 'word to translate' => 'translation'
            'Dashboard' => 'Foo',
            'Add new' => 'Bar'
     );
$translated = str_ireplace(  array_keys($words),  $words,  $translated );
return $translated;
}

参考文献:Wordpress replace all ocurrances of the string in admin

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66220947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档