php 在一个字符串中查找关键字并加亮显示
2011-7-11 杜世伟
<?php
/*
* Created on 2011-5-16
* author 孤独求学人
*/
/*
* $word 要查看的字符串
* $find_word 要查找的字符串
* $color 默认的颜色
* 返回加亮查找的字符串
*/
function find_word($word,$find_word,$color='#ffff00'){
if(is_array($find_word)){
foreach($find_word as $k=>$new_word){
$pattern[$k] = $new_word;
$replace[$k] = "<font style='background-color:".$color.";'>\\1</font>";//\\1 符号来表示逆向引用
}
}else{
$pattern = "/\b($find_word)\b/is";
$replace = "<font style='background-color:".$color.";'>\\1</font>";//\\1 符号来表示逆向引用
}
//preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit])
return preg_replace($pattern,$replace,$word);
}
?>
<?
$str = "good study good up,good up";
$result = find_word($str,"good","#ff0000");
print_r($result);
?>
热门日志
分类
- Django(4)
- ssdb(1)
- Mac(7)
- C(1)
- memcache(1)
- Python(32)
- Vim(8)
- sed(2)
- ansible(3)
- awk(4)
- shell(3)
- about(1)
- git(9)
- bat(4)
- svn(0)
- docker(1)
- Tornado(1)
- go(2)
- 架构(19)
- Vue(1)
- game(2)
- Html(6)
- Java(8)
- Mysql(37)
- Ajax(2)
- Jsp(1)
- Struts(8)
- Linux(73)
- JavaScript(39)
- Staruml(0)
- Mouth(1)
- Php(102)
- Windows(8)
- Message(48)
- Lua(10)
- Compute(1)
- Redis(7)
- Nginx(12)
- Jquery(1)
- Apache(1)
- cocos2d-x(8)
最新日志
- 成为架构师,如何真正具备“系统思维”?
- DHCP(Dynamic Host Configuration Protocol) 动态主机配置协议
- 从技术专家到战略领袖:成就技术总监的路径与思维
- python 如何读取超大的文件
- python requests 模块
- 如何给自己充电?
- 告别2024,迎接2025:深耕梦想,向前而行
- linux 的 dns 缓存,NSCD 服务
- The following untracked working tree files would be overwritten by checkout
- insecure connection not allowed,产生原因及如何解决