正则一小问 http://www.fixdown.com/article/article/1841.htm 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <script language="javascript">var str,str2,str3;str="sdfsdfasfsf[code]我是中国人[/code]sdfss";re=/(\[code\])(.*)(\[\/code\])/g ;str2=str.replace(re,"<FONT color=#d56000>"+"$2"+"</FONT>");document.write(str2)</script>不过上面的只能解决带有一对[code]的。多对就会取最两边的了。。 <?php$s = <<< TEXTstring0string1[code]string2[/code]string3string4TEXT;echo preg_replace("/\[code\](.+)\[\/code\]/sU","<font color=red>\\1</font>",$s);?> (唠叨) 大哥解决了提取的目的,但没有实现将其高亮显示,我说的高亮指的是形如highlight_string的效果,请问能否实现? 还有,如果我的string2是一段HTML代码或PHP代码,上面的将不会达到目的,比如将string2替换成<? echo "hello world";?> preg_replace+e 或者 preg_replace_callback一个例子<?php$s = <<< TEXTstring0string1[code]string2 <?php echo "hello"; ?>[/code]string3string4TEXT;function foo($m){ return highlight_string(stripslashes($m));}echo preg_replace("/\[code\](.+)\[\/code\]/sUe","foo('\\1')",$s);?> to ccterran(iwind)你似乎解决了高亮显示的效果,但显示的位置不正确这样做的话,就将[code][/code]之间的内容置首显示了 $str=[code]some code[/code]//highlight codepreg_match_all("/\[code\](.*)\[\/code\]/iUs","$code_str",$c_match,PREG_PATTERN_ORDER);$array_num=count($c_match[0]);for ($i=0;$i<$array_num;$i++) { $match_all=$c_match[0][$i]; $h_str=$c_match[1][$i]; $h_str=highlight_string($h_str,"ture"); $h_str="Code:<br />=======begin=======<br />".$h_str."<br />========end=======<br />"; $code_str=str_replace($match_all,$h_str,$code_str); }echo $code_str; test:http://surfchen.wegame.com/show_post.php?fid=15&pid=f_360 建议一个群...... 请教一个Update 的技巧 调用存放在mysql中的图片,如何在客户端展示在页面上 一个简单的问题中文url php下载文件 图片不能打开 web打印的时候设置好默认打印机之后为什么还会跳出选择打印机啊? 下载了一些网站的全站php代码,怎么无从入手修改? 购物车的完全实现.SOS!!!!通过处理session数组来实现. 用php直接生成数据图表怎么做??:) 怎样在php中嵌入ActiveX控件? 如何能取得某网站的一些信息? 问PHP连接MYSQL的问题
var str,str2,str3;
str="sdfsdfasfsf[code]我是中国人[/code]sdfss";
re=/(\[code\])(.*)(\[\/code\])/g ;
str2=str.replace(re,"<FONT color=#d56000>"+"$2"+"</FONT>");
document.write(str2)
</script>
不过上面的只能解决带有一对[code]的。多对就会取最两边的了。。
$s = <<< TEXT
string0
string1
[code]
string2
[/code]string3
string4
TEXT;echo preg_replace("/\[code\](.+)\[\/code\]/sU","<font color=red>\\1</font>",$s);
?>
<?php
$s = <<< TEXT
string0
string1
[code]
string2 <?php echo "hello"; ?>
[/code]string3
string4
TEXT;
function foo($m)
{
return highlight_string(stripslashes($m));
}
echo preg_replace("/\[code\](.+)\[\/code\]/sUe","foo('\\1')",$s);
?>
你似乎解决了高亮显示的效果,但显示的位置不正确
这样做的话,就将[code][/code]之间的内容置首显示了
preg_match_all("/\[code\](.*)\[\/code\]/iUs","$code_str",$c_match,PREG_PATTERN_ORDER);
$array_num=count($c_match[0]);
for ($i=0;$i<$array_num;$i++)
{
$match_all=$c_match[0][$i];
$h_str=$c_match[1][$i];
$h_str=highlight_string($h_str,"ture");
$h_str="Code:<br />=======begin=======<br />".$h_str."<br />========end=======<br />";
$code_str=str_replace($match_all,$h_str,$code_str);
}
echo $code_str;
http://surfchen.wegame.com/show_post.php?fid=15&pid=f_360