如何将HTML标记解析为纯文本?我想实现类似Facebook或Twitter的功能 [英] how to parse HTML tags to plain text?I want to achive something like facebook or twitter
问题描述
例如:我有这个字符串
@[1234:peterwateber] <b>hello</b> <div>hi!</div> http://stackoverflow.com
我想像这样将其转换为HTML:
I want to convert it into HTML like this:
<a href="1234">@peterwateber</a> <b>hello</b> <div>hi!<divb>
<a href="http://sackoverflow.com">http://stackoverflow.com</a>
我正在使用QueryPath,并且我有这段代码,您可以在其中将"@@ [123:peterwateber]" 中的文本输出到" 123 ", peterwateber ".
I'm using QueryPath, and I have this code where you can get the texts from "@[123:peterwateber]" to be outputted to "123 and peterwateber" respectively.
The code to do that is:
$hidden_input = "@[1234:peterwateber] <b>hello</b> <div>hi!</div> http://stackoverflow.com";
preg_match('#@\[(\w+)\:(\w+)\]#', $hidden_input, $m); //returns 123,peterwateber
我想要实现的是获得这种输出:
What I'm trying to achieve is to have this kind of output:
我正在将Hawkee的插件用于jQuery自动完成 http://www.hawkee. com/snippet/9391/
I'm using Hawkee's plugin for jQuery autocomplete http://www.hawkee.com/snippet/9391/
推荐答案
我不确定是否有专门为此功能的特定功能,但是您可以执行以下操作: 在链接(a href)
I'm not entirly sure if there is a specific function just for that but what you can do is this: in example of the link (a href)
$raw = "@[1234:peterwateber]"
$thingtoreplace = ("@[");
$firstpass = str_replace($thingtoreplace, "<a href='", $raw);
$raw2 = $firstpass
$thingtoreplace = (":");
$secondpass = str_replace($thingtoreplace, "'>", $raw1);
$raw3 = $second
$thingtoreplace = ("]");
$secondpass = str_replace($thingtoreplace, "'</a>", $raw3);
我知道这似乎很乏味,但应该可以解决.如果它没有帮助,请不要对我评价……我花了时间
I know it seems tedious but it should do the trick. If its not helpful then please dont rate me down... I spent time on this
这篇关于如何将HTML标记解析为纯文本?我想实现类似Facebook或Twitter的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!