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)
- 架构(18)
- Vue(1)
- game(2)
- Html(6)
- Java(8)
- Mysql(37)
- Ajax(2)
- Jsp(1)
- Struts(8)
- Linux(72)
- 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)
最新日志
随机日志
- php-fpm通过request_slowlog_timeout检查哪个脚本执行时间长
- 永远不要在 Linux 执行的 10 个最危险的命令
- js 实现php中sprintf函数
- Js中window.parent ,window.top,window.self 详解
- Warning: Unknown: Unable to allocate memory for pool. in Unknown on line 0 Warning: require_once(): Unable to allocate memory for pool
- Jquery跨域请求php获取数据之jsonp
- apache 开启gzip压缩和http缓存的配置方法
- linux gz 解压缩