-
-
/**
- * 添加文章关键词,内链
- * by bbs.it-home.org
- */
- $link = array(
- ‘百度,http://www.baidu.com/’,
- ‘dedecms,http://bbs.it-home.org/’,
- );
- $str = ‘在百度中搜索dedecms’;
- $out = keylink ( $str, $link, 1 );
- //$str 原始字符 $link,替换链接数组, 3替换次数
- echo $out;
- function _sortDesc($a, $b) {
- return (strlen ( $a [0] ) }
- function keylink($str, $link, $count = 1) {
- $linkDefs = $link;
- $linkMap = array ();
- foreach ( $linkDefs as $row ) {
- $linkMap [] = explode ( ‘,’, $row );
- }
- foreach ( $linkMap as $row ) {
- $str = preg_replace ( ‘/(s*)(‘ . $row [0] . ‘)(s*)/sui’, ‘${2}’, $str );
- }
- usort ( $linkMap, ‘_sortDesc’ );
- $tmpKwds = array ();
- foreach ( $linkMap as $i => $row )
- {
- list ( $kwd, $url ) = $row;
- for($j = $i + 1; $j {
- $subKwd = $linkMap [$j] [0];
- //如果包含其他关键字,暂时替换成其他字符串
- if (strpos ( $kwd, $subKwd ) !== false)
- {
- $tmpKwd = ‘{‘ . md5 ( $subKwd ) . ‘}’;
- $kwd = str_replace ( $subKwd, $tmpKwd, $kwd );
- $tmpKwds [$tmpKwd] = $subKwd;
- }
- }
- //把文字替换成链接
- $str = preg_replace ( ‘/(‘ . $row [0] . ‘)/sui’, ‘’ . $kwd . ‘’, $str, $count );
- }
- //把代替子关键字的字符串替换回来
- foreach ( $tmpKwds as $tmp => $kwd ) {
- $str = str_replace ( $tmp, $kwd, $str );
- }
- return $str;
- }
- ?>
复制代码
|