将关键字转换为超链接(带撇号的问题)

时间:2019-02-16 02:01:50

标签: php hyperlink apostrophe

我有一个脚本,可以自动将我的Wordpress网站中的关键字转换为超链接。效果很好,但是我要超链接的关键字之一中带有撇号-并且代码不会处理该关键字。

我尝试过:

"'Key\'word2' => '<a href="https://www.test.com/2.php" 
target="_blank">Key\'word2</a>'

但是那行不通。

有人可以为此建议解决方法吗? 干杯

/***** KEYWORDS to links FUNCTION *****/

function link_words( $text ) {
$replace = array(
'Keyword1' => '<a href="https://www.test.com/1.php" 
target="_blank">Keyword1</a>',
'Key'word2' => '<a href="https://www.test.com/2.php" 
target="_blank">Key'word2</a>'
);
$text = str_replace( array_keys($replace), $replace, $text );
return $text;
}
add_filter( 'the_content', 'link_words' );
add_filter( 'the_excerpt', 'link_words' );

1 个答案:

答案 0 :(得分:1)

尝试使用heredoc。它就像引号或双引号一样工作。您可以使用任何字母,而不仅仅是ABC。

<<<ABC 'Key\'word2' => '<a href="https://www.test.com/2.php" target="_blank">Key\'word2</a>' ABC>>>
相关问题