How to remove text between tags in php?


Question

Despite using PHP for years, I've never really learnt how to use expressions to truncate strings properly... which is now biting me in the backside!

Can anyone provide me with some help truncating this? I need to chop out the text portion from the url, turning

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

into

<a href="link.html"></a>
1
18
5/3/2011 4:34:11 PM

Accepted Answer

$str = preg_replace('#(<a.*?>).*?(</a>)#', '$1$2', $str)
30
9/1/2009 11:20:21 AM

Using 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;
?>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon