如何在php中删除标签之间的文本?

2022-08-30 21:35:32

尽管使用PHP多年,但我从未真正学会如何使用表达式来正确截断字符串......它现在正在后面咬我!

任何人都可以为我提供一些帮助来截断这一点吗?我需要从URL中切出文本部分,转动

<a href="link.html">text</a>

<a href="link.html"></a>

答案 1
$str = preg_replace('#(<a.*?>).*?(</a>)#', '$1$2', $str)

答案 2

使用 SimpleHTMLDom

<?php
// example of how to modify anchor innerText
include('simple_html_dom.php');

// get DOM from URL or file
$html = file_get_html('http://www.example.com/');

//set innerText to null for each anchor
foreach($html->find('a') as $e) {
    $e->innerText = null;
}

// dump contents
echo $html;
?>

推荐