php - 如何在某个 html 标记之前和之后去除文本

标签 php html text strip strip-tags

基本上我想采取类似的方式:

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

并删除所有的texthere和moretext here只是离开表格

最佳答案

你需要找到<table>strpos 的位置然后使用 substr删除到此为止的文本。然后对 </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>

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

关于php - 如何在某个 html 标记之前和之后去除文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6652723/

相关文章:

javascript - .wrap() 元素内数组中的单词

php - 如何在不使用 CodeIgniter 中的输入的情况下一次插入多行?

html - 使用 CSS 网格对齐极端的元素

java - 如何在文件的特定行之后插入文本

c# - 使用 C# 搜索和替换文本文件中的值

php - 我的 php 代码无法获取 url 中的值

php - 如何使用 php 将树状数组输出到列表中?

javascript - 手机锁时可以触发播放音频吗

html - 滑动悬停链接动画的自动宽度

Java RegEx 删除英文单词但排除一些单词