0

I would like to do something like this:

<my_tag_name>
text..
any_text
</my_tag_name>

but the problem appears what if the user puts the content like this:

<my_tag_name>
text..
any_text
</my_tag_name>
</my_tag_name>

So I replaced < with <'

function content($string, $tagname)
{
 $pattern = "/<$tagname>([\w\W]*?)<\/$tagname>/";
 $preg_match($pattern, $string, $matches);
 return str_replace("<'", "<", $matches[1]);
}

function replace($string)
{
  return str_replace("<", "<'", $string);  
}

The goal is to have custom tags and any kind of text as a content. Is this correct approach? I tried it and it works. But then again I remembered the same principle is in html but there you can't put let's say < div> my content < div> < /div>.

I also wanted to have like this:

tag: reserved_64_characters
tag2: reserved_64_characters

How are these things implemented in XML? Is there also some escaping/replacing. I would like to do that any content can be inserted I mean any characters(also < tag_name >..).

On http://www.w3schools.com/xml/xml_cdata.asp

Notes on CDATA sections:

A CDATA section cannot contain the string "]]>"

broadband
  • 3,266
  • 6
  • 43
  • 73
  • 3
    One of the best posts ever: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags In essence: Do not use regex for grammars! – SJuan76 Jul 28 '11 at 07:46
  • 1
    +1 to "don't use regex for grammars" – Nemoden Jul 28 '11 at 07:50

3 Answers3

2

You should escape your user input with htmlentities. That's it.

Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
1

Take a look at CDATA which is exactly what you need if I understand your question right (which I doubt, unfortunately).

<my_tag_name>
<![CDATA[text..
any_text
</my_tag_name>]]>
</my_tag_name>

so

text..
any_text
</my_tag_name>

is value of my_tag_name now

Nemoden
  • 8,816
  • 6
  • 41
  • 65
0

you could use php's htmlentities() or strip_tags() to parse/scrub the user input ...

designosis
  • 5,182
  • 1
  • 38
  • 57
  • I applies only to html tags, but here I have my own tags, there could be , ... or you mean just the concept og htmlentities() – broadband Jul 28 '11 at 08:06