# Regex: exclude within tags

**URL:** <https://forum.kirupa.com/t/regex-exclude-within-tags/235850>\
**Category:** programming\
**Created:** [August 15, 2007, 9:34am UTC](https://forum.kirupa.com/t/regex-exclude-within-tags/235850 "2007-08-15T09:34:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aneurin\_Mark\_II](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/aneurin_mark_ii/32/1564_2.png) [@Aneurin\_Mark\_II](https://forum.kirupa.com/u/Aneurin_Mark_II)\
**Post date:** [August 15, 2007, 9:34am UTC](https://forum.kirupa.com/t/regex-exclude-within-tags/235850/1 "2007-08-15T09:34:08Z")

</div>

[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:**

```auto
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:

```php
$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:

```php
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:

```php
/[^<(input).*]\[b\](.*?)\[\/b\][.*[^>]]/

```
