Log in

View Full Version : Resolved PCRE formatting



james438
11-13-2009, 04:07 AM
PCRE is a rather processor heavy calculation for PHP as is GD image processing. I have a program on my site that formats the sample code on my site to look something like the formatted code seen in forums. I do this by encapsulating the code in CODE brackets, which is then inserted into the database. When the code is retrieved for viewing it is formatted using PCRE, but I do try to use str_replace() where possible. If I want to edit the text then the code is not PCRE formatted, so that I can see the CODE markings. The CODE markings are what sets the code aside for formatting.

Does this sound like the right way to go about using my PCRE for formatting my code example scripts or am I just wasting resources?

james438
11-13-2009, 10:44 PM
My question is now moot. I found a way to reformat the PCRE and replace it all with variations of str_replace().

Here is an abridged version of what my code now looks like


<?php
$i=1;
$summary = explode("CODE",$summary);
$a=count($summary);
while ($i<$a)
{
#### change the [CODE] areas to display returns.

$summary[$i] = htmlentities($summary[$i]);
$summary[$i] = str_replace("\r\n","<br>",$summary[$i]); // goes to next line and to the leftmost margin
$summary[$i] = str_replace(" ","&nbsp;&nbsp;",$summary[$i]);

$i=$i+2;
}
$i=0;
while ($i<$a)
{
if ($i%2 == 0)
{

####changes the non [code] sections to display returns
$k="**";
$summary[$i]=str_replace("\r\n","<br>",$summary[$i]);
$summary[$i]=str_replace(' ',"$k",$summary[$i]);
}
else $summary[$i]= "<div class=withborder>$summary[$i]</div>"; #### adds CSS to the [CODE] areas.
$i=$i+1;
}
$summary = implode (" ",$summary);
echo "$summary";
?>