Hi,
The first solution that comes to my mind looks like this :
- extract all the codes
- remove the codes, replacing them with a special marker, that will not be affected by your string manipulations -- that marker has to be really special (and you could verify it's not present in the input string, btw)
- do your manipulations on the string
- put back the codes, where there are markers now
In code, it could be something like this :
(sorry, it's quite long -- and I didn't include any check ; it's up to you to add those)
$str = <<<A
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sodales lacus et erat accumsan consectetur. Sed lacinia enim vitae erat suscipit fermentum. Quisque lobortis nisi et lacus imperdiet ac malesuada dui imperdiet. <pre><code>ThIs Is
CoDe 1</code></pre>Donec vestibulum commodo quam rhoncus luctus. Nam vitae ipsum sed nibh dignissim condimentum. Sed ultrices fermentum dapibus. Vivamus mattis nisi nec enim convallis quis aliquet arcu accumsan. Suspendisse potenti. Nullam eget fringilla nunc. Nulla porta justo justo. Nunc consectetur egestas malesuada. Mauris ac nisi ipsum, et accumsan lorem. Quisque interdum accumsan pellentesque. Sed at felis metus. Nulla gravida tincidunt tortor, <pre><code>AnD cOdE 2</code></pre>nec aliquam tortor ultricies vel. Integer semper libero eu magna congue eget lacinia purus auctor. Nunc volutpat ultricies feugiat. Nullam id mauris eget ipsum ultricies ullamcorper non vel risus. Proin volutpat volutpat interdum. Nulla orci odio, ornare sit amet ullamcorper non, condimentum sagittis libero. <pre><code>aNd
CoDe
NuMbEr 3
</code></pre>Ut non justo at neque convallis luctus ultricies amet.
A;
var_dump($str);
// Extract the codes
$matches = array();
preg_match_all('#<pre><code>(.*?)</code></pre>#s', $str, $matches);
var_dump($matches);
// Remove the codes
$str_nocode = preg_replace('#<pre><code>.*?</code></pre>#s', 'THIS_IS_A_NOCODE_MARKER', $str);
var_dump($str_nocode);
// Do whaterver you want with $str_nocode
$str_nocode = strtoupper($str_nocode);
var_dump($str_nocode);
// And put back the codes :
$str_codes = $str_nocode;
foreach ($matches[0] as $code) {
$str_codes = preg_replace('#THIS_IS_A_NOCODE_MARKER#', $code, $str_codes, 1);
}
var_dump($str_codes);
I've tried with :
- code on one line,
- code on 2 lines,
- and code on multiple lines
Note : you should really test more than I did -- but this could give you a first idea...
Hope this helps :-)
As a side note : generally, parsing HTML with regexes is considered bad practice, and often leads to troubles... Maybe using something like DOMDocument::loadHTML
could be an idea worth having a look ?