在html中用正则替换,如何保证不替换掉html标签,则仅仅替换掉字符?

conis 2010-02-11 03:24:58
例如在对如何下的html进行替换:

<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>


我希望把所有的字母A全部进行加粗显示,但又不能影响到html的显示结果,正确的结果如下:

<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>
...全文
343 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
江南昆虫 2010-03-08
  • 打赏
  • 举报
回复
PHP版
<?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;
?>
江南昆虫 2010-03-08
  • 打赏
  • 举报
回复
JS版
<!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>
YHL27 2010-03-07
  • 打赏
  • 举报
回复
it is very good!
programbin 2010-03-07
  • 打赏
  • 举报
回复
呵呵,如果可以记得给分哦。
programbin 2010-03-07
  • 打赏
  • 举报
回复
上面那个有问题。这个用正则不太好实现哦。我自己写了个。

$str = <<<STR
<div class="abc">
<a href="#a">go to a<div>go to span</div></a>
<a><div>这是一a个测试</div></a>
<img src="images/a.gif" alt="this is image a" />
<p>
<span>caption</span>
<span class="content">Application</span>
</p>
</div>
STR;

function getTag(&$html, &$pos, $len)
{
$sp = $pos;
$rs = '<';
$pos++;
$c = $html[$pos];
while(true)
{
if($c === '<')
{
$pos = $sp;
return false;
}
$rs .= $c;
$pos++;
if($pos >= $len)
{
return false;
}
$c = $html[$pos];
if($c === '>')
{
break;
}
}

$rs .= '>';

return $rs;
}

function replace($html)
{
$len = strlen($html);

$rs = '';
$pos = 0;
$c = $html[$pos];
while(true)
{
if($c === '<')
{
$gr = getTag($html, $pos, $len);
if($gr !== false)
{
$rs .= $gr;
}
}
else if($c === 'a' || $c === 'A')
{
$rs .= '<b>' . $c . '</b>';
}
else
{
$rs .= $c;
}

$pos++;
if($pos >= $len)
{
break;
}
$c = $html[$pos];
}

return $rs;
}

$str = replace($str);

echo $str;
programbin 2010-03-07
  • 打赏
  • 举报
回复
$str = <<<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>
STR;

$reg = '/<.+?>.+<.+?>/i';

preg_match_all($reg, $str, $array);

$array = array_unique($array[0]);

$len = count($array);

for($i = 0;$i < $len;$i++)
{
preg_match('/<.+?>(.+)<.+?>/i', $array[$i], $a);
$inner = preg_replace('/(a)/i','<b>\1</b>', $a[1]);
$outer = preg_replace('/(<.+?>).+(<.+?>)/i', '\1' . $inner . '\2', $a[0]);
$str = str_replace($a[0], $outer, $str);
}

echo $str;
motol 2010-02-11
  • 打赏
  • 举报
回复

$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;

conis 2010-02-11
  • 打赏
  • 举报
回复
上面还有一个a没有替换

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧