2011-04-05 139 views
0
function replace_text_wps($text){ 
     $replace = array(
      // 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS' 
      'wordpress' => '<a href="#">wordpress</a>', 
      'excerpt' => '<a href="#">excerpt</a>', 
      'function' => '<a href="#">function</a>' 
     ); 
     $text = str_replace(array_keys($replace), 
    $replace, $text); 
     return $text; } 

    add_filter('the_content','replace_text_wps'); 
    add_filter('the_excerpt','replace_text_wps'); 

这段代码用来替换一些单词,为什么他使用add_filter()函数两次,他错了吗?为什么add_filter()被应用两次?

另外,行$text = str_replace(array_keys($replace), $replace, $text)是什么意思?

回答

1
$text = str_replace(array_keys($replace), $replace, $text); 

替换为$都给键在$文本字符串

这个代码只是过滤两个不同的字符串替换。

1
$text = str_replace(array_keys($replace), $replace, $text); 

此行搜索从$replace所有数组键和与它们各自的值替换它们。

它基本上foreach($replace as $s => $r) $text = str_replace($s, $r, $text);

1
add_filter('the_content','replace_text_wps'); 
add_filter('the_excerpt','replace_text_wps'); 

他将过滤器应用于帖子的内容以及摘录较短/更好的方式(通常是从后主体部分相分离。另外填写)。一般而言,您只能在博客列表中使用其中的一种,因此他将其应用于两者都涵盖了所有基础。

$text = str_replace(array_keys($replace), $replace, $text); 

// 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS' 

然后,他只是在做一个字符串替换:http://php.net/manual/en/function.str-replace.php

基本上,如果你的帖子内容有任何下列词语wordpress, excerpt, excerpt它将取代与被包裹arounf字的链接词。

相关问题