不会写正则真痛苦。。
我想实现,
如果输入的是
转为<span class="style4" ><a href="show.php?id=5">点击</a></span>/**************************************************/里面的id:5 中的5是变量,style:4中的4也是变量.请问这个正则如何实现?
我想实现,
如果输入的是
转为<span class="style4" ><a href="show.php?id=5">点击</a></span>/**************************************************/里面的id:5 中的5是变量,style:4中的4也是变量.请问这个正则如何实现?
解决方案 »
- 不同表单的值怎么才能一同得到?
- 新手有个不太懂的,为啥现在写PHP都不用DW...
- 上面一段php代码,如何把 $post_title , 前面加上一个字符 苹果电脑,该怎么修改呀
- 奇怪的post, 麻烦一下?
- 《提问的智慧,智慧的提问》不得不提醒一下
- 请问怎样才能把一些session变量值写到apache的日志文件中?
- PHP的树型菜单
- php信息安全的解决??(100分)
- 那里有比较好的php+mysql虚拟主机空间,价格在500-800元/年???
- 关于session的问题
- MS Sans Serif 字体阴谋, MS 也太歹毒了
- ☆☆☆PHP中webservice调用的问题【麻烦大家帮帮忙】
// \\2 是一个逆向引用的例子,其在 PCRE 中的含义是
// 必须匹配正则表达式本身中第二组括号内的内容,本例中
// 就是 ([\w]+)。因为字符串在双引号中,所以需要
// 多加一个反斜线。
$html = "<b>bold text</b><a href=howdy.html>click me</a>";preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);for ($i=0; $i< count($matches[0]); $i++) {
echo "matched: ".$matches[0][$i]."\n";
echo "part 1: ".$matches[1][$i]."\n";
echo "part 2: ".$matches[3][$i]."\n";
echo "part 3: ".$matches[4][$i]."\n\n";
}
?> 本例将输出: matched: <b>bold text</b>
part 1: <b>
part 2: bold text
part 3: </b>matched: <a href=howdy.html>click me</a>
part 1: <a href=howdy.html>
part 2: click me
part 3: </a>
preg_match("/\[img\](([a-z]+):([0-9]),)*([a-z]+):([0-9])\[\/img\]/i", $str, $matches);
print_r($matches);
for($i=2; $i<count($matches); $i+=2) {
$att[$matches[$i]]=$matches[$i+1];
}
echo "<span class=\"style{$att['style']}\" ><a href=\"show.php?id={$att['id']}\">点击 </a> </span>";
请问这样如何实现???
{
$att_arr=explode(',', $att_str);
foreach($att_arr AS $att_line) {
list($attname,$attvalue)=explode(':',$att_line);
$atts[$attname]=$attvalue;
}
return "<span class=\"style{$atts['style']}\" ><a href=\"show.php?id={$atts['id']}\">点击 </a> </span>";
}
$str = "other contentother contentother content";
echo preg_replace("/\[img\](([a-z]+:[0-9],?)+)\[\/img\]/ie", "replaceit('\\1')", $str)
那这个更有通用性。
$string = 'i want to make a test ! ,go';
$pattern = '/\[img\]id:(\d+),style:(\d+)\[\/img\]/is';
$replacement ='<span class="style$1" ><a href="show.php?id=$2">点击</a></span>';
echo preg_replace($pattern, $replacement, $string);