Im working on my project where people can add php code snippets to my database. To display them, use SyntaxHighlighter. Im using preg_replace to avoid all the <brackets in the preliminary tags. This is necessary, therefore syntaxhighlighter displays the code correctly. It works fine on php tags, etc.
This is my code for visualizing input from my database:
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); }
After the brackets have been escaped, I add the class to the pre tag to activate the marker.
In my database, the code is stored as follows:
<pre><?php foreach ($tutorial as $row) { echo "<h1>".$row['title']."</h1>"; echo $this->content_model->renderPre($row['intro']); echo $this->content_model->renderPre($row['body']); } ?></pre>
Now on my actual page, where the code is retrieved from the database and displayed in the marker, this is the output:
<?php foreach ($tutorial as $row) { echo \"<h1>\".$row['title'].\"</h1>\"; echo $this->content_model->renderPre($row['intro']); echo $this->content_model->renderPre($row['body']); } ?>
In the line where the H1 tag is located, it adds a few extra slashes (\) I don't know why this is. This should have something to do with / ise in the render function.
Does anyone know how to fix this?
Thanks!!
edit:
Code to retrieve data from a database:
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(); }