求忽略HTML标签的替换字符串正则表达式 有现成函数strip_tags看看手册吧 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼上误会我的意思了,我不是要去除HTML标签,我是要在替换的时候仍然不改动HTML标签 <?php$str = '<font color="red">red hat</font>';$str = preg_replace('/(<[^\/>]+>)([^<]+)(<\/[^>]+>)/', "\\1blue hat\\3", $str);print htmlspecialchars($str);?> 输出:<font color="red">blue hat</font> $myString = "<font color=\"red\">red hat</font><aaa color=\"red\">aaa</aaa>";$resultString = preg_replace("/(<[^>]*>)[\s\w]+(<\/[^>]*>)/", "\\1你想改的放这里\\2", $myString); $str = "<font color=\"red\">red hat</font> <aaa color=\"red\">aaa</aaa><img alt='hot'>sdfsdf</img> ";echo preg_replace("/(?<=>)\s*\w[^<]+\s*(?=<)/is","BLUE HAT",$str); 不好意思,前段时间一直没时间。看来大家还是不懂我的意思。我是要在一个HTML文件里面替换除了HTML标签以外的指定关键字。如这段<font color="red">red hat前段时间一直没时间</font><span id="red">看来大家red 还是不red懂我的意思</span>这段文字要把RED替换成blue,结果应该是这样:<font color="red">blue hat前段时间一直没时间</font><span id="red">看来大家blue 还是不blued懂我的意思</span>请问有没有高手可以解决? $s='<font color="red">red hat前段时间一直没时间</font><span id="red">看来大家red 还是不red懂我的意思</span>';preg_match_all("/>([^<]+?)</",$s,$m);print_r(array_map("tr",$m[1]));function tr($h) { return str_replace("red","blue",$h);} 谢谢Aylazhang,你的方法很接近了!我用你写的代码打出了一下的结果,但是如何把替换之后的字符串接回原来的HTML?是不是需要再写一个正则匹配那些HTML,然后将两个结果的数组拼起来?array(2) { [0]=> array(3) { [0]=> string(39) ">red hat前段时间一直没时间 <" [1]=> string(3) "> <" [2]=> string(48) ">看来大家red 还是不red懂我的意思 <" } [1]=> array(3) { [0]=> string(37) "red hat前段时间一直没时间 " [1]=> string(1) " " [2]=> string(46) "看来大家red 还是不red懂我的意思 " }}array(3) { [0]=> string(38) "blue hat前段时间一直没时间 " [1]=> string(1) " " [2]=> string(48) "看来大家blue 还是不blue懂我的意思 "} $s= ' <font color="red" >red hat前段时间一直没时间 </font > <span id="red" >看来大家red 还是不red懂我的意思 </span > ';preg_match_all("/>([^<]+?)</is",$s,$m);var_dump($m);$r = array_map("tr",$m[1]);var_dump($r);function tr($h) { return str_replace("red","blue",$h);}preg_match_all("/(<[^>]+?>)(.*?)/is" , $s , $m1);var_dump($m1);输出array(2) { [0]=> array(3) { [0]=> string(39) ">red hat前段时间一直没时间 <" [1]=> string(3) "> <" [2]=> string(48) ">看来大家red 还是不red懂我的意思 <" } [1]=> array(3) { [0]=> string(37) "red hat前段时间一直没时间 " [1]=> string(1) " " [2]=> string(46) "看来大家red 还是不red懂我的意思 " }}array(3) { [0]=> string(38) "blue hat前段时间一直没时间 " [1]=> string(1) " " [2]=> string(48) "看来大家blue 还是不blue懂我的意思 "}array(3) { [0]=> array(4) { [0]=> string(21) "<font color="red" >" [1]=> string(8) "</font >" [2]=> string(18) "<span id="red" >" [3]=> string(8) "</span >" } [1]=> array(4) { [0]=> string(21) "<font color="red" >" [1]=> string(8) "</font >" [2]=> string(18) "<span id="red" >" [3]=> string(8) "</span >" } [2]=> array(4) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" }}接起来似乎很困难 $s= '<font color="red" >red hat前段时间一直没时间 </font ><span id="red">看来大家red 还是不red懂我的意思 </span >';echo preg_replace("/(?<!=\")red(?!\")/","BLUE",$s);这个不能保证你的tag外面的也有 ="red 这样的字符串。比如:<font color="red"> sdfsf ="red 一直 </font> 这样的情况。 $keyword2 = "<font style='color:red'>$keywords</font>";$pattern='/(?!<[^>]*)('.$keywords.')(?![^<]*>)/i';$str=preg_replace($pattern,$keyword2,$str);$str=preg_replace("@&(\w{0,6})?({$keyword2})(\w{0,6})?;@","&$1$keywords$3;",$str); 一个关于时间问题 php里 自定义错误 首次在csdn上被人恶评,发帖散分。 今天头一次接触php,有些超级基础的问题。请大家耐心 一个正则替换的问题,请大家帮忙!~ update 失效,为什么? pdo 问题,熟悉的来! 简单问题 mysql 不能執行這樣的 sql 語句嗎,請大家幫忙看看!!select customer_id from so where custoemr_id in (select customer_id from so limi php中有没有象ASP的mappath(),将虚拟路径变为实际路径的函数? PHP+Oracle,WEB页面显示乱码 求一个网上书店的网页
$resultString = preg_replace("/(<[^>]*>)[\s\w]+(<\/[^>]*>)/", "\\1你想改的放这里\\2", $myString);
echo preg_replace("/(?<=>)\s*\w[^<]+\s*(?=<)/is","BLUE HAT",$str);
preg_match_all("/>([^<]+?)</",$s,$m);
print_r(array_map("tr",$m[1]));
function tr($h) {
return str_replace("red","blue",$h);
}
array(2) {
[0]=>
array(3) {
[0]=>
string(39) ">red hat前段时间一直没时间 <"
[1]=>
string(3) "> <"
[2]=>
string(48) ">看来大家red 还是不red懂我的意思 <"
}
[1]=>
array(3) {
[0]=>
string(37) "red hat前段时间一直没时间 "
[1]=>
string(1) " "
[2]=>
string(46) "看来大家red 还是不red懂我的意思 "
}
}
array(3) {
[0]=>
string(38) "blue hat前段时间一直没时间 "
[1]=>
string(1) " "
[2]=>
string(48) "看来大家blue 还是不blue懂我的意思 "
}
preg_match_all("/>([^<]+?)</is",$s,$m);
var_dump($m);
$r = array_map("tr",$m[1]);
var_dump($r);
function tr($h) {
return str_replace("red","blue",$h);
}preg_match_all("/(<[^>]+?>)(.*?)/is" , $s , $m1);
var_dump($m1);输出array(2) {
[0]=>
array(3) {
[0]=>
string(39) ">red hat前段时间一直没时间 <"
[1]=>
string(3) "> <"
[2]=>
string(48) ">看来大家red 还是不red懂我的意思 <"
}
[1]=>
array(3) {
[0]=>
string(37) "red hat前段时间一直没时间 "
[1]=>
string(1) " "
[2]=>
string(46) "看来大家red 还是不red懂我的意思 "
}
}
array(3) {
[0]=>
string(38) "blue hat前段时间一直没时间 "
[1]=>
string(1) " "
[2]=>
string(48) "看来大家blue 还是不blue懂我的意思 "
}
array(3) {
[0]=>
array(4) {
[0]=>
string(21) "<font color="red" >"
[1]=>
string(8) "</font >"
[2]=>
string(18) "<span id="red" >"
[3]=>
string(8) "</span >"
}
[1]=>
array(4) {
[0]=>
string(21) "<font color="red" >"
[1]=>
string(8) "</font >"
[2]=>
string(18) "<span id="red" >"
[3]=>
string(8) "</span >"
}
[2]=>
array(4) {
[0]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(0) ""
}
}
接起来似乎很困难
$s= '<font color="red" >red hat前段时间一直没时间 </font ><span id="red">看来大家red 还是不red懂我的意思 </span >';
echo preg_replace("/(?<!=\")red(?!\")/","BLUE",$s);
这个不能保证你的tag外面的也有 ="red 这样的字符串。
比如:
<font color="red"> sdfsf ="red 一直 </font> 这样的情况。
$keyword2 = "<font style='color:red'>$keywords</font>";
$pattern='/(?!<[^>]*)('.$keywords.')(?![^<]*>)/i';
$str=preg_replace($pattern,$keyword2,$str);
$str=preg_replace("@&(\w{0,6})?({$keyword2})(\w{0,6})?;@","&$1$keywords$3;",$str);