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

问题描述 投票:1回答:1

我有一个脚本,可以自动将我的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' );
php hyperlink apostrophe
1个回答
1
投票

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

<<<ABC 'Key\'word2' => '<a href="https://www.test.com/2.php" target="_blank">Key\'word2</a>' ABC>>>
© www.soinside.com 2019 - 2024. All rights reserved.