How to remove text between tags in php?

Asked
Active3 hr before
Viewed126 times

9 Answers

remove
90%

Meta Stack Overflow ,Stack Overflow em Português,Stack Overflow en español,Thanks for contributing an answer to Stack Overflow!

$str = preg_replace('#(<a.*?>).*?(</a>)#', '$1$2', $str)
load more v
88%

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!,Another way would be to write a regular expression to match your criteria. But in order to get your problem solved quickly the string functions will do...,You could use substring in combination with stringpos, eventhough this is not a very nice approach.,This avoids the expense of capture groups using a lazy quantifier, the fullstring restarting \K and a "lookahead".

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>
load more v
72%

strip_tags — Strip HTML and PHP tags from a string,(PHP 4, PHP 5, PHP 7, PHP 8), This function tries to return a string with all NULL bytes, HTML and PHP tags stripped from a given string. It uses the same tag stripping state machine as the fgetss() function. , HTML comments and PHP tags are also stripped. This is hardcoded and can not be changed with allowed_tags.

Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>
65%

Extract and returns the content between tags:,The preg_match() function is the best option to extract text between HTML tags with REGEX in PHP. If you want to get content between tags, use regular expressions with preg_match() function in PHP. You can also extract the content inside element based on class name or ID using PHP.,The example code snippet shows how to get the content inside the specific div (HTML element) using PHP. The following code uses preg_match() with the regular expression to extract text or HTML content from under tags using PHP.,How can we extract the text between all H2 tags with whatever attributes they have (like id , class) from an html ?

The example code snippet shows how to get the content inside the specific div (HTML element) using PHP. The following code uses preg_match() with the regular expression to extract text or HTML content from under tags using PHP.

$htmlContent = '<div class="my-con">Content goes here...</div>';preg_match('/<div class="my-con">(.*?)<\ /div>/s', $htmlContent, $match);
load more v
75%

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!,The real solution would be utilize a language parser, but that's obviously overkill. So try with adding some heuristics to avoid removing wrong occourences.,You cannot do this reliably. There is no guarantee that // at any position in a file indicates a comment in PHP context. It might very well be contained in a string for example.,Can anyone provide me with some help truncating this? I need to chop out the text portion from the url, turning

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>
load more v
40%

And I need it to work with html-text to. How can I transform text only between html tags? Don't touch tag name and all tag attributes. E.g. I need:,May be preg_match() would be better? Any suggestions? It would be nice to provide working php-code.,I have a function doit() that transforms text that way:,If you add any files,it will delete all existing files related to this answer-(only this answer)

I have a function doit() that transforms text that way:

I like berries - > I LikE BerrieS.

And I need it to work with html-text to. How can I transform text only between html tags? Don't touch tag name and all tag attributes. E.g. I need:

<p class="super green" style="height: auto">I LikE BerrieS</p>

but not:

<P ClasS="SupeR GreeN" StylE="HeighT: AutO">I LikE BerrieS</P>
load more v
22%

echo strip_tags("Hello <b>world!</b>");
load more v
60%

How to Extract Content Between HTML Tags in PHP,In this tutorial, we are going to see how to extract content between HTML tags in PHP. preg_match() function is the easiest way to extract text between HTML tags with REGEX in PHP. If you want to get content between tags, use regular expressions with the preg_match() function in PHP. You can also extract the content inside the element based on the class name or ID.,The code snippet below shows how to get the content inside the div block. The following code uses preg_match() with a regular expression to extract text or HTML.  ,Home » php » How to Extract Content Between HTML Tags in PHP

How to Extract Content Between HTML Tags in PHP
<?php
	$html = '<div class="test">Lorem ipsum dolor sit amet.</div>';

	preg_match('/<div class="test">(.*?)<\/div>/s', $html, $match);
?>
load more v
48%

To remove all HTML tags from a string, use the strip_tags function. This function tries to return a string with all NULL bytes, HTML, and PHP tags stripped from a given string.,Remove HTML tags from a string in PHP,Remove all attributes from HTML tags in PHP,Remove HTML tags from a string except p, b, i and br in PHP

$text = '<div class="test">
   <p>Clean <a href="#">text</a></p>
</div>';

$cleanText = strip_tags($text);

Other "remove-undefined" queries related to "How to remove text between tags in php?"