>我在 php 调用的模板文件中有一个文本块使用以下代码:
echo $playlist->description
该字段包含 HTML 链接,当该字段的结果显示为使用 Twitter API 创建的推文框中的内容时,我想删除这些链接。
显示文本示例:
<a href="http://google.com">http://google.com</a> some text here.
我希望实现以下结果:
some text here
我已经考虑过用正则表达式、preg_replace
和其他一些方法来实现这一点。但是,我还没有弄清楚如何合并到我现有的字符串中。
我尝试了以下方法,但没有成功,
echo $playlist->description->$str = preg_replace('#(<a.*?>).*?(</a>)#', '$1$2', $str)
我自称是php菜鸟,我花了几周的时间试图实现我的目标,所以任何帮助都非常感谢。我觉得我真的很接近!帮助!!!:)
此致敬意话筒
您也可以在不使用正则表达式的情况下执行此操作!
$a = end(explode('</a>' , $playlist->description ));
这将为您提供</a>
之后的一切
echo preg_replace('#.*?</a>'s*(.*)#', "$1", $playlist->description);
干杯
我做了一个函数来删除HTML标签及其内容:
功能:
<?php
function strip_tags_content($text, $tags = '', $invert = FALSE) {
preg_match_all('/<(.+?)['s]*'/?['s]*>/si', trim($tags), $tags);
$tags = array_unique($tags[1]);
if(is_array($tags) AND count($tags) > 0) {
if($invert == FALSE) {
return preg_replace('@<(?!(?:'. implode('|', $tags) .')'b)('w+)'b.*?>.*?</'1>@si', '', $text);
}
else {
return preg_replace('@<('. implode('|', $tags) .')'b.*?>.*?</'1>@si', '', $text);
}
}
elseif($invert == FALSE) {
return preg_replace('@<('w+)'b.*?>.*?</'1>@si', '', $text);
}
return $text;
}
?>
示例文本:$text ="带标签的示例文本";
strip_tags($text)的结果:带标签的示例文本
strip_tags_content($text)的结果:文本与
strip_tags_content($text, '') 的结果:示例文本
strip_tags_content($text, '', TRUE) 的结果;带标签的文本
我希望有人有用:)