[size=“7”]Please see post below this one.[/size]
Hi,
I am having a problem with a regular expression I am using to translate simplified markup (based on BBCode) into HTML. It is not the regex specifically that has caused the problem, rather it is the system I use.
System logic:
1. store page output as variable $t['pagetemplate']
2. translate all markup in $t['pagetemplate'] to HTML using simplecode() function
3. echo translated
This is no problem for an ordinary page. All the markup is translated fine using the following template:
$pre = "'\[p\](.*?)\[/p\]'is";
$post = '<p>\\1</p>';
preg_replace($pre, $post, $target);
However, while coding the ACP, I have hit a stumbling block; if I use any markup text within a form object, then that, too, is translated. The user can of course enter original markup into the form, but if I want to reload a POST variable into a form object or textarea that contains markup, it translates it to HTML - not only replacing the old code but also breaking the form layout (since there are quotes in the HTML that aren’t in the original markup).
I have been trying to modify my regex to exclude any tags within an tag (I will deal with textarea once I’ve sorted this out…)
This is my current simplecode() function where $pre is an array of partial regexes, $post is the HTML output and $target is the target string:
function simplecode($pre, $post, $target) {
$pre_n = array();
$pre_def = '/[^<(input).*]£[.*[^>]]/'; // Regex template: exclude forms
foreach ($pre as $p):
$q = str_replace("£", $p, $pre_def);
array_push($pre_n, $q);
endforeach;
$result = preg_replace($pre_n, $post, $target);
return $result;
}
I’m pretty sure my function, including the foreach, works correctly. I doubt the regex does, though. Would anyone care to suggest a regex that actually does what I need, ie. recognise all markup except anything within an tag? Thanks - this would be so helpful.
Edit: if it’s any help, this is an example generated regex:
/[^<(input).*]\[b\](.*?)\[\/b\][.*[^>]]/