0

I'm getting content from database:

$data="some markup <pre> <code> some code </code> </pre> some markup"

What I want to do is following

If $data has <pre> <code> </code> </pre> inside, then do something and pass some code through htmlspecialchars() function. Then merge as it was before. Something like:

$data="some markup <pre> <code> htmlspecialchars(some code) </code> </pre> some markup"

Can't figure out how to do it..

heron
  • 3,611
  • 25
  • 80
  • 148

3 Answers3

0

Sanitize it on the way in. Then you only have ONE chance to screw it up and get your site hacked, instead of having to get it right everywhere you use the data.

Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39
0

I think you probably want to parse the HTML with DOM, then find all instances where there's code between <pre> and <code> tags.

Then you can take that text and do a str_replace() on it.

Matt
  • 6,993
  • 4
  • 29
  • 50
-1
$data = 'one<pre><code>two</code></pre>three';
$code = preg_match('~^(.*)<pre><code>(.*)</code></pre>(.*)$~', $data, $matches);
var_dump($matches);
Elastic Lamb
  • 353
  • 2
  • 12
  • 1
    [The pony. He comes.](http://stackoverflow.com/a/1732454/1338999) Stop trying to parse a non-regular language (a.k.a. HTML) with regular expressions. It DOES NOT WORK. – Matt Aug 13 '12 at 19:31
  • It's no HTML. Furthermore you can use regular expressions to extract information like that. – Elastic Lamb Aug 13 '12 at 19:46