Home  >  Article  >  Backend Development  >  php给文章加关键字连接,文章内容自动加链接效果 怎样控制链接次数?该如何处理

php给文章加关键字连接,文章内容自动加链接效果 怎样控制链接次数?该如何处理

WBOY
WBOYOriginal
2016-06-13 13:32:44786browse

php给文章加关键字连接,文章内容自动加链接效果 怎样控制链接次数?
我现在已经实现了php输出html的指定关键词链接到指定的URL,但是有一个问题是通篇所有的关键词都会链接,我想控制它链接的次数或者只让它链接文章中出现的第一个关键词,请问如何实现?已经实现的关键词自动链接代码如下:
function keylink($cont){
$keys = array(
array('关键词1','http://链接1')
);
foreach($keys as $nkeys){
$cont = preg_replace("/($nkeys[0])/iU","$nkeys[0]",$cont);
}
return $cont;
}
求php大大!!!

------解决方案--------------------
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

第四个参数,limit限制为1.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn