$str=preg_replace("/[attach]/is""111"$str); 这一句怎么把111替换成可以执行的PHP代码

WBOY
Release: 2016-06-13 12:05:35
Original
1056 people have browsed it

$str=preg_replace("/\[attach\]/is","111",$str); 这一句如何把111替换成可以执行的PHP代码
$str=preg_replace("/\[attach\]/is","111",$str);  这一句如何把111替换成可以执行的PHP代码

比如$str=preg_replace("/\[attach\]/is","$str=preg_replace(" width="280"/>",$str);  结果出来后成字符了
------解决方案--------------------
这个是BBCODE的PHP替换。你应该是想实现类似DISCUZ的那种附件BBCODE。
要想回调可以使用 preg_replace_callback。

<br /><?php <br /><br />//author: default7<default7#zbphp.com<br />$str = preg_replace_callback("/\[attach\](\d+)\[\/attach\]/isUe",function($attachId = 0){<br />	//todo <br />	$row = $db->query("select * from tb where id=$id ")->row();<br />	return '<img  src="'.$row['pic'].'" / alt="$str=preg_replace("/[attach]/is""111"$str);  这一句怎么把111替换成可以执行的PHP代码" >';<br />},$str);<br /><br />
Copy after login

原理如上,具体代码自己测试一下。




------解决方案--------------------
function get($id) {<br />  //$value = 这里是相关操作的代码<br />  return $value;<br />}<br />$str = preg_replace("/\[(attach)\]/ise", "get('$1')", $str);
Copy after login

也可以
function get($r) {<br />  //$value = 这里是相关操作的代码<br />  return $value;<br />}<br />$str = preg_replace_callback("/\[(attach)\]/is", "get", $str);
Copy after login



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template