带有锚点<a>标签、http:// 或URL的PHP注释以及其余的HTML标签是纯文本
php comment with a an anchor <a> tag, http:// or url and the rest of the html tags are plain text
我有这个输出:<a href="#">sdfsdfsd</a>gdsfgsdfgsdgsdggsdfg
问题是此函数将所有 html 标签转换为纯文本,除了那些带有 url 的标签,如 www.facebook.com (将其转换为 <a href="www.facebook.com">www.facebook.com</a>
):
function validate_text($text = '') {
// This method is used internally as a FILTER_CALLBACK
if (mb_strlen($text, 'utf8') < 1)
return false;
// Encode all html special characters (<, >, ", & .. etc) and convert
//$str = nl2br(htmlspecialchars($text));
$str = htmlspecialchars($text);
// the new line characters to <br> tags:
// Remove the new line characters that are left
$str = str_replace(array(chr(10), chr(13)), '', $str);
$text = preg_replace('#(script|about|applet|activex|chrome):#is', "''1:", $str);
$ret = ' ' . $text;
$ret = preg_replace("#(^|['n ])(['w]+?://['w'#$%&~/.'-;:=,?@'[']+]*)#is", "''1<a href='"''2'" target='"_blank'">''2</a>", $ret);
$ret = preg_replace("#(^|['n ])((www|ftp)'.['w'#$%&~/.'-;:=,?@'[']+]*)#is", "''1<a href='"http://''2'" target='"_blank'">''2</a>", $ret);
$ret = preg_replace("#(^|['n ])([a-z0-9&'-_.]+?)@(['w'-]+'.(['w'-'.]+'.)*['w]+)#i", "''1<a href='"mailto:''2@''3'">''2@''3</a>", $ret);
//$ret = preg_replace("#^*@([)([0-9-])(])#is", "''1<a href='"http://''2'" target='"_blank'">''2</a>", $ret);
$ret = substr($ret, 1);
return $ret;
}
我想要从<a href="#">something</a> <small>hello</small>
到<a href="#">something</a>sadfsafasf <small> hello </small>
strip_tags()
将从字符串中删除所有 html 标签,但您指定为允许的标签除外。
编辑:根据您在下面的评论;
要将包含 HTML 标记的字符串显示为文本,而不让浏览器解析需要使用 htmlspecialchars()
或 htmlentities()
的标记。然后,要将@mention替换为链接,请使用正则表达式并preg_replace()
。
这是您追求的(我认为)的示例:-
<?php
$string = "Some text with some <span>HTML tags in it</span> and a @mention to someone";
// Turn special characters into html entities
$new_string = htmlspecialchars($string);
// Replace @mention with a link
$output = preg_replace("/@('w+)/",'<a href="http://www.example.com/profiles/$1">$1</a>',$new_string);
// Will produce 'Some text with some <span>HTML tags in it</span> and a <a href="http://www.example.com/profiles/mention">mention</a> to someone'
echo $output;
?>
相关文章:
- 在<页眉>标签
- Ckeditor-plugin:插入虚假元素add不情愿<p>标签前后
- iframe正在添加标签,需要删除它们
- 如何自动调整标签的高度以适应内容
- Jquery标签插件粘贴问题
- Twitter引导程序Typeahead-Id&标签
- Javascript中的备选注释方法
- html5视频中的Youtube类型注释
- Highcharts-如何在自定义格式化程序中获得默认的y轴标签格式化程序
- 如何防止 TinyMCE 将 CDATA 添加到<脚本>标签中,并注释掉<样式>标签
- 使用 alt 标签优雅地注释图像
- 如何注释掉混合了服务器标签的 JavaScript 代码块
- 带有锚点标签、http:// 或URL的PHP注释以及其余的HTML标签是纯文本
- 谷歌图表选项隐藏y标签和注释
- 在HTML注释标签中获取文本
- 如何在Javascript或jQuery的注释中包装标签
- 从3升级到PHP 5.5之后.x,它似乎试图解析我的JavaScript注释(嵌入在一个PHP包含文件的脚本标签)
- 在注释标签中包含javascript代码"
- 强制Jade忽略脚本标签中的注释
- JavaScript Regex替换两个注释标签之间的字符串