自定义标记值格式
我想做这样的事情:
<my_tag_name>
text..
any_text
</my_tag_name>
但是问题出现了,如果用户将内容像这样:
<my_tag_name>
text..
any_text
</my_tag_name>
</my_tag_name>
所以我替换了 <与 <'
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);
}
目标是将自定义标签和任何类型的文本作为内容。这是正确的做法吗?我尝试过并且有效。但话又说回来,我记得 html 中也有同样的原理,但你不能把 <分区>我的内容 <分区> < /div>。
我也想这样:
tag: reserved_64_characters
tag2: reserved_64_characters
这些东西是如何用XML实现的?是否还有一些转义/替换。 我希望可以插入任何内容,我的意思是任何字符(也
关于 http://www.w3schools.com/xml/xml_cdata.asp
关于 CDATA 的注释sections:
CDATA 节不能包含字符串“]]>”
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 "]]>"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用 htmlentities 转义用户输入。就是这样。
You should escape your user input with htmlentities. That's it.
看看
CDATA
如果我理解,这正是您所需要的你的问题是对的(不幸的是,我对此表示怀疑)。如此
现在
my_tag_name
的值也是Take a look at
CDATA
which is exactly what you need if I understand your question right (which I doubt, unfortunately).so
is value of
my_tag_name
now您可以使用 php 的 htmlentities() 或 strip_tags() 来解析/擦除用户输入...
you could use php's htmlentities() or strip_tags() to parse/scrub the user input ...