Php Preg_更换未正确更换
我在做我的一个项目,人们可以在我的数据库中添加一些php代码。 使用SyntaxHighlighter显示这些im。Im使用preg_replace转义pre标签中的所有<括号。这是必需的,以便syntaxhighlighter正确地呈现代码。它可以很好地处理php标记和其他东西 这是我从数据库中呈现输入的代码:Php Preg_更换未正确更换,php,mysql,preg-replace,Php,Mysql,Preg Replace,我在做我的一个项目,人们可以在我的数据库中添加一些php代码。 使用SyntaxHighlighter显示这些im。Im使用preg_replace转义pre标签中的所有
public function renderPre($input) // Function to escape html brackets within PRE tags.
{
$temp = preg_replace('/<pre>(.*?)<\/pre>/ise', "'<pre>' . htmlspecialchars('$1') . '</pre>'", $input);
return str_replace('<pre>', '<pre class=\'brush: php\'>', $temp);
}
public function renderPre($input) // Function to escape html brackets within PRE tags.
{
$input = htmlspecialchars_decode($input);
$temp = preg_replace('/<pre>(.*?)<\/pre>/ise', "'<pre>' . htmlspecialchars('$1') . '</pre>'", $input);
return str_replace('<pre>', '<pre class=\'brush: php\'>', $temp);
}
试一试
public function renderPre($input)//用于转义PRE标记中的html括号的函数。
{
$input=htmlspecialchars\u解码($input);
$temp=preg_replace('/(.*?)/ise',“'.htmlspecialchars('$1').”,$input);
返回stru_替换(“”,,$temp);
}
谢谢大家的回答。我通过向renderpre函数添加stripslashes使其正常工作:
public function renderPre($input)//用于转义PRE标记中的html括号的函数。
{
$temp=preg_replace('/(.*?)/ise',“'.htmlspecialchars('$1').”,$input);
返回stru_替换('','',带斜线($temp));
}
您能给我们展示一下用于向数据库写入数据和从数据库读取数据的代码吗?添加了读取数据的代码,但尚未编写插入数据的代码,手动将其添加到数据库中我觉得很好(请参阅)。另一方面,为什么有这个$temp
?为什么不从一开始就替换为“.htmlspecialchars(“$1”).”“
”并返回它呢?(例:)那是因为数据库中没有类,我将按照你建议的方式进行尝试it@DavidEricsson是的,但是只有替换项有一个类,而不是regexpi asume u mean htmlspecialchars,因为unhtml special chars不起作用。当我使用你的代码时,它根本不能正确地呈现:p谢谢你的回答tho!
<?php
foreach ($tutorial as $row)
{
echo \"<h1>\".$row['title'].\"</h1>\";
echo $this->content_model->renderPre($row['intro']);
echo $this->content_model->renderPre($row['body']);
}
?>
public function get_tutorial()
{
$sql = "Select
tutorial.*,
category.name,
category.slug As slug1
From
tutorial Inner Join
category On tutorial.category_id = category.id
WHERE tutorial.slug = '".$this->uri->segment(3)."'
";
$query = $this->db->query($sql);
return $query->result_array();
}
public function renderPre($input) // Function to escape html brackets within PRE tags.
{
$input = htmlspecialchars_decode($input);
$temp = preg_replace('/<pre>(.*?)<\/pre>/ise', "'<pre>' . htmlspecialchars('$1') . '</pre>'", $input);
return str_replace('<pre>', '<pre class=\'brush: php\'>', $temp);
}
public function renderPre($input) // Function to escape html brackets within PRE tags.
{
$temp = preg_replace('/<pre>(.*?)<\/pre>/ise', "'<pre>' . htmlspecialchars('$1') . '</pre>'", $input);
return str_replace('<pre>', '<pre class=\'brush: php\'>', stripslashes($temp));
}