Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove text between tags in php?

Tags:

string

regex

php

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>
like image 460
MrFidge Avatar asked Sep 01 '09 11:09

MrFidge


People also ask

How to remove tags from string in PHP?

PHP provides an inbuilt function to remove the HTML tags from the data. The strip_tags() function is an inbuilt function in PHP that removes the strings form HTML, XML and PHP tags. It accepts two parameters. This function returns a string with all NULL bytes, HTML, and PHP tags stripped from a given $str.

How do you remove HTML tag from data in PHP?

The strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped. This cannot be changed with the allow parameter.

How do I remove text tags in HTML?

The HTML tags can be removed from a given string by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.

What is strip HTML?

stripHtml( html ) Changes the provided HTML string into a plain text string by converting <br> , <p> , and <div> to line breaks, stripping all other tags, and converting escaped characters into their display values.


2 Answers

$str = preg_replace('#(<a.*?>).*?(</a>)#', '$1$2', $str)
like image 61
Amber Avatar answered Sep 28 '22 00:09

Amber


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;
?>
like image 39
karim79 Avatar answered Sep 28 '22 00:09

karim79