php自动给文章加关键词链接的函数代码
复制代码 代码如下:
$link = array(
'百度,http://www.baidu.com/',
'服务器软件,http://s.jb51.net/',
'脚本之家,http://www.jb51.net/',
);
$str = '在百度中搜索服务器软件就可以到脚本之家提供的软件
夏季不再穿长筒袜近年成为一种时尚,甚至到了秋天也不例外。
专家提出,其实这种时尚不利于美腿。据悉,久坐久站缺少运动等原因易导致静脉曲张,尤其是长期站立工作的人,容易引起下肢静脉本身扩张、延长或静脉瓣膜损坏以至发病。具体表现为腿部肿胀,下肢静脉犹如蚯蚓状弯曲或结节成团,皮肤发紫,特别是踝和小腿内侧更为严重。
专家指出,长期站立工作的人或多或少都有一些下肢静脉曲张的表现,只是轻重程度不一样。下肢静脉曲张是一种治疗起来比较麻烦的疾病,严重的需要手术,所以最好的方法还是防患于未然,在症状尚不明显的时候,就采取防范措施,穿长筒弹力袜就是一种很好的方法,可帮助血液进入较大且较深处的静脉。
保持理想体重,可减少静脉曲张的机会;避免紧身衣物,穿医用弹力袜,以免使血液聚积在腿部;小心食用避孕药,某些避孕药可能引起这种问题。';
$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('/(
}
usort($linkMap, '_sortDesc');
$tmpKwds = array();
foreach($linkMap as $i=>$row) {
list($kwd, $url) = $row;
for($j=$i+1; $j
//如果包含其他关键字,暂时替换成其他字符串
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;
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
