如何去除某个 html 标签之前和之后的文本

发布于 2024-11-19 17:43:07 字数 256 浏览 1 评论 0原文

基本上我想采取类似的操作:

textheretextheretexthere
<table>
<tr><td>heres some stuff</td><td>and some morestuff</td></tr>
</table>
moretextheremoretexthere

并删除此处的所有文本和此处的更多文本,只留下表格

Basically I want to take something like:

textheretextheretexthere
<table>
<tr><td>heres some stuff</td><td>and some morestuff</td></tr>
</table>
moretextheremoretexthere

and remove all the texthere and moretext here just leaving the table

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏见 2024-11-26 17:43:07

您需要使用 strpos 找到 位置,然后使用 substr 删除此处的文本。然后对于 < code>

$string = 'textheretextheretexthere<table><tr><td>heres some stuff</td><td>and some morestuff</td></tr></table>moretextheremoretexthere';

$table_pos = strpos($string,'<table>');
$string = substr($string,$table_pos);
//Your string now is <table><tr><td>heres some stuff</td><td>and some morestuff</td></tr></table>moretextheremoretexthere

$endtable_pos = strpos($string,'</table>')+8;//added 8 so i wont exclude </table>
$clean_string = substr($string,0,$endtable_pos);
//Your string now is <table><tr><td>heres some stuff</td><td>and some morestuff</td></tr></table>

当然,这根本不完美,我知道,但你得到了提示,你可以努力改进它,也许最终会得到一个可以帮助你解决问题的函数。

You need to find <table> position with strpos and then use substr to remove the text to this point.and then the same for </table>

$string = 'textheretextheretexthere<table><tr><td>heres some stuff</td><td>and some morestuff</td></tr></table>moretextheremoretexthere';

$table_pos = strpos($string,'<table>');
$string = substr($string,$table_pos);
//Your string now is <table><tr><td>heres some stuff</td><td>and some morestuff</td></tr></table>moretextheremoretexthere

$endtable_pos = strpos($string,'</table>')+8;//added 8 so i wont exclude </table>
$clean_string = substr($string,0,$endtable_pos);
//Your string now is <table><tr><td>heres some stuff</td><td>and some morestuff</td></tr></table>

Of course this is not perfect at all,i know but you got the hint,you can work on improving it and maybe end up with a function that helps you solve your problem.

逆流 2024-11-26 17:43:07
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';    

echo strip_tags($text);
echo "\n";

// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';    

echo strip_tags($text);
echo "\n";

// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文