Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP regular expression to remove tags in HTML document

Say I have the following text

..(content).............
<A HREF="http://foo.com/content" >blah blah blah </A>
...(continue content)...

I want to delete the link and I want to delete the tag (while keeping the text in between). How do I do this with a regular expression (since the URLs will all be different)

Much thanks

like image 555
Señor Reginold Francis Avatar asked Sep 01 '09 22:09

Señor Reginold Francis


People also ask

How do you remove HTML tags from data 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 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 a regex in PHP?

A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this search pattern to describe what you are searching for. A regular expression can be a single character, or a more complicated pattern.


1 Answers

This will remove all tags:

preg_replace("/<.*?>/", "", $string);

This will remove just the <a> tags:

preg_replace("/<\\/?a(\\s+.*?>|>)/", "", $string);
like image 184
nickf Avatar answered Oct 03 '22 07:10

nickf