Note that there are some explanatory texts on larger screens.

plurals
  1. POPreg_replace not replacing correctly
    primarykey
    data
    text
    <p>Im working on a project of mine, where people can add pieces of php code into my database. To display these im using SyntaxHighlighter. Im using preg_replace to escape all the &lt; brackets within pre tags. This is needed so syntaxhighlighter renders the code properly. It works fine on php tags and stuff..</p> <p>This is my code to render the input from my database: </p> <pre><code> public function renderPre($input) // Function to escape html brackets within PRE tags. { $temp = preg_replace('/&lt;pre&gt;(.*?)&lt;\/pre&gt;/ise', "'&lt;pre&gt;' . htmlspecialchars('$1') . '&lt;/pre&gt;'", $input); return str_replace('&lt;pre&gt;', '&lt;pre class=\'brush: php\'&gt;', $temp); } </code></pre> <p>Once the brackets have been escaped i add a class to the pre tag to activate the highlighter.</p> <p>In my database the code is stored like this :</p> <pre><code>&lt;pre&gt;&lt;?php foreach ($tutorial as $row) { echo "&lt;h1&gt;".$row['title']."&lt;/h1&gt;"; echo $this-&gt;content_model-&gt;renderPre($row['intro']); echo $this-&gt;content_model-&gt;renderPre($row['body']); } ?&gt;&lt;/pre&gt; </code></pre> <p>Now on my actual page, where the code is retrieved from the database and displayed in the highlighter, this is the output :</p> <pre><code>&lt;?php foreach ($tutorial as $row) { echo \"&lt;h1&gt;\".$row['title'].\"&lt;/h1&gt;\"; echo $this-&gt;content_model-&gt;renderPre($row['intro']); echo $this-&gt;content_model-&gt;renderPre($row['body']); } ?&gt; </code></pre> <p>At the line where the H1 tag is, it adds a few extra slashes ( \ ) I dont know why it does this. It must have something to do with the /ise in the render function.</p> <p>Does anyone have any idea on how to fix this?</p> <p>Thanks!!</p> <p>edit :</p> <p>The code to retrieve stuff from the database :</p> <pre><code> 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-&gt;uri-&gt;segment(3)."' "; $query = $this-&gt;db-&gt;query($sql); return $query-&gt;result_array(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload