21,887
社区成员
发帖
与我相关
我的任务
分享
<div class="abc">
<a href="#a">go to a</a>
<img src="images/a.gif" alt="this is image a" />
<p>
<span>caption</span>
<span class="content">application</span>
</p>
</div>
<div class="abc">
<a href="#a">go to <b>a</b></a>
<img src="images/a.gif" alt="this is image a" />
<p>
<span>c<b>a</b>ption</span>
<span class="content"><b>a</b>ndroid <b>a</b>pplication</span>
</p>
</div>
<?php
$str = 'A<div class="abc">
<a href="#a">go to a</a>
<img src="images/a.gif" alt="this is image a" />
<p>
<span>caption</span>
<span class="content">application</span>
</p>
</div>aa
';
$str = preg_replace_callback('/(?<=^|>).+(?=<|$)/U',create_function('$ms','return preg_replace("/a/i","<b>$0</b>",$ms[0]);'),$str);
echo $str;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="editplus" />
<meta name="author" content="JnKc" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<style type="text/css">
*{font-size:22px;}
</style>
</head>
<body>
<div class="abc">
<a href="#a">go to a</a>
<img src="images/a.gif" alt="this is image a" />
<p>
<span>caption</span>
<span class="content">application</span>
</p>
</div>
</body>
</html>
<script type="text/javascript">
document.body.innerHTML = document.body.innerHTML.replace
(/(>)([^<]+)(<)/gim,
function f2c($0,$1,$2,$3){
return($1+$2.replace(/a/gim,'<b>a</b>')+$3);
}
);
</script>
$str = '<div class="abc">
<a href="#a">go to a</a>
<img src="images/a.gif" alt="this is image a" />
<p>
<span>caption</span>
<span class="content">application</span>
</p>
</div>
';
$tempStr = str_replace("<","||<",$str); //在html标签前加上分割符||(分割符根据实际情况更改)
$tempStr = strip_tags($tempStr); //得到去html标签的字符窜
$strArray = explode('||', $tempStr); //将字符窜按分割符进行分割
foreach($strArray as $v)
{
$tempStr = str_replace('a', '<b>a</b>', $v); //替换a为<b>a</a>
$str = str_replace($v, $tempStr, $str); //替换原字符每个html标签内容
}
echo $str;