Php 如何在不影响alt属性中的关键字的情况下替换Wordpress中链接的关键字
我试图将帖子内容中的关键词替换为链接,但是,我意识到img标签的alt属性中的关键词也受到了影响。 有解决办法吗?顺便说一句,我在functions.php中使用的代码是:Php 如何在不影响alt属性中的关键字的情况下替换Wordpress中链接的关键字,php,wordpress,filter,hook,Php,Wordpress,Filter,Hook,我试图将帖子内容中的关键词替换为链接,但是,我意识到img标签的alt属性中的关键词也受到了影响。 有解决办法吗?顺便说一句,我在functions.php中使用的代码是: function replace_text_wp($text){ $replace = array( 'keyword1' => '<a href="http://demo.com/" rel="bookmark" title="keyword1">keyword1</a>',
function replace_text_wp($text){
$replace = array(
'keyword1' => '<a href="http://demo.com/" rel="bookmark" title="keyword1">keyword1</a>',
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
function replace_text_wp($text){
$replace=数组(
'关键字1'=>'',
);
$text=str_replace(数组_键($replace),$replace,$text);
返回$text;
}
添加_过滤器(“_内容”、“替换_文本_wp”) 不久前,我不得不做一些类似的事情,请尝试将此作为您的函数-它可能需要一些修改和调整,因为我正在根据您的示例调整它,而无需测试
function replace_text_wp($the_content){
//Break up the content into parts - allowing us to just edit content and not tags
$parts = preg_split('/(<(?:[^"\'>]|"[^"<]*"|\'[^\'<]*\')*>)/', $the_content, -1, PREG_SPLIT_DELIM_CAPTURE);
// Loop through the blocks and just edit the content
for ($i=0, $n=count($parts); $i<$n; $i+=2) {
// Check if any of our keywords are within the content
// If so then make the keywords a link
$parts[$i] = str_replace('keyword1', '<a href="http://demo.com/" rel="bookmark" title="keyword1">keyword1</a>', $parts[$i]);
}
}
// Now put it all back together
$the_content = implode('', $parts);
return $the_content;
}
function replace_text_wp(_内容){
//将内容分解为多个部分-允许我们只编辑内容而不编辑标签
$parts=preg_split('/(不久前,我不得不做一些类似的事情,尝试将此作为您的函数-它可能需要一些修改和调整,因为我正在根据您的示例调整它,而无需测试
function replace_text_wp($the_content){
//Break up the content into parts - allowing us to just edit content and not tags
$parts = preg_split('/(<(?:[^"\'>]|"[^"<]*"|\'[^\'<]*\')*>)/', $the_content, -1, PREG_SPLIT_DELIM_CAPTURE);
// Loop through the blocks and just edit the content
for ($i=0, $n=count($parts); $i<$n; $i+=2) {
// Check if any of our keywords are within the content
// If so then make the keywords a link
$parts[$i] = str_replace('keyword1', '<a href="http://demo.com/" rel="bookmark" title="keyword1">keyword1</a>', $parts[$i]);
}
}
// Now put it all back together
$the_content = implode('', $parts);
return $the_content;
}
function replace_text_wp(_内容){
//将内容分解为多个部分-允许我们只编辑内容而不编辑标签
$parts=preg_split('/(谢谢Simon,正则表达式只做了一点修改,它工作得非常好,请您将此标记为helpfull或答案please Alan:)-可能会添加您需要的正则表达式,因为我对它感兴趣。现在仍然是新的,无法标记或投票,我使用的正则表达式很简单:code
$parts=preg_split(“/()/”,$text,-1,PREG_SPLIT_DELIM_CAPTURE);code
在我的情况下,我仍然希望在tag等中有关键字来替换Hanks Simon,只需在正则表达式上做一点更改,而且效果非常好。请将此标记为helpfull或答案please Alan:)-可能会添加您需要的正则表达式,因为我很想看到它。现在仍然是新的,无法标记或投票,我使用的正则表达式很简单:code
$parts=preg_split(“/()/”,$text,-1,preg_split_DELIM_CAPTURE);code
在我的情况下,我仍然希望在标记等中有关键字可以替换