dz的手机版不显示图片,所以我想自己写一个 显示人页面。 function get($r) { $value = SELECT * FROM `bbs_forum_attachment_$row[tableid]` where tid =id return $value; } $str = preg_replace_callback("/\[(attach)\]/is", "get", $str); where tid =id 这个ID如何得到
你 print_r($r) 就可看到
function get($r) { $value = "SELECT * FROM `bbs_forum_attachment_9` where tid =$r" return $value; } $str = preg_replace_callback("/\[(attach)\]/is", "get", $str);错误提示:Parse error: syntax error, unexpected T_RETURN
呵呵;我改成 function get($r) { $value = "SELECT * FROM `bbs_forum_attachment_9` where tid =$r"; return $value; } $str = preg_replace_callback("/\[(attach)\]/is", "get", $str); 可是前天输出 SELECT * FROM `bbs_forum_attachment_9` where tid =Array26588我想要的是SELECT * FROM `bbs_forum_attachment_9` where tid =26588 不要Array 怎么弄
我改成单引号前台输出 SELECT * FROM `bbs_forum_attachment_9` where tid =$r是不是这段php语句没有执行,直接转成文本了
26588 从哪里来的? 在get方法里面 print_r($r); 得到什么?
改成$str = preg_replace_callback("/\[attach\](.+?)\[\/attach\]/is", "get", $str); 之后输出:SELECT * FROM `bbs_forum_attachment_9` where tid =Array
要想回调可以使用 preg_replace_callback。
<?php //author: default7<default7#zbphp.com
$str = preg_replace_callback("/\[attach\](\d+)\[\/attach\]/isUe",function($attachId = 0){
//todo
$row = $db->query("select * from tb where id=$id ")->row();
return '<img src="'.$row['pic'].'" />';
},$str);
原理如上,具体代码自己测试一下。
修正:
$str = preg_replace_callback("/\[attach\]\s*(\d+)\s*\[\/attach\]/isUe",function($match){
//todo
$id = $match[1];
$row = $db->query("select * from tb where id=$id ")->row();
return $row ? '<img src="'.$row['pic'].'" />' : '';
},$str);
修正:
$str = preg_replace_callback("/\[attach\]\s*(\d+)\s*\[\/attach\]/isUe",function($match){
//todo
$id = $match[1];
$row = $db->query("select * from tb where id=$id ")->row();
return $row ? '<img src="'.$row['pic'].'" />' : '';
},$str);
出现错误提示:Parse error: syntax error, unexpected T_FUNCTION in
指向$str = preg_replace_callback("/\[attach\]\s*(\d+)\s*\[\/attach\]/isUe",function($match){这一行
//$value = 这里是相关操作的代码
return $value;
}
$str = preg_replace("/\[(attach)\]/ise", "get('$1')", $str);
也可以function get($r) {
//$value = 这里是相关操作的代码
return $value;
}
$str = preg_replace_callback("/\[(attach)\]/is", "get", $str);
function get($r) {
$value = SELECT * FROM `bbs_forum_attachment_$row[tableid]` where tid =id
return $value;
}
$str = preg_replace_callback("/\[(attach)\]/is", "get", $str); where tid =id 这个ID如何得到
function get($r) {
$value = "SELECT * FROM `bbs_forum_attachment_9` where tid =$r"
return $value;
}
$str = preg_replace_callback("/\[(attach)\]/is", "get", $str);错误提示:Parse error: syntax error, unexpected T_RETURN
呵呵;我改成
function get($r) {
$value = "SELECT * FROM `bbs_forum_attachment_9` where tid =$r";
return $value;
}
$str = preg_replace_callback("/\[(attach)\]/is", "get", $str);
可是前天输出
SELECT * FROM `bbs_forum_attachment_9` where tid =Array26588我想要的是SELECT * FROM `bbs_forum_attachment_9` where tid =26588 不要Array 怎么弄
忘了写明,我写的那个要 PHP 5.5+