I have text that is displayed from a database and I have managed to remove the first h3 tag in the text by using the following code but I would also like to remove the two <br> tags that are before the paragraph of text

<h2 class="productsummaryheading mb-0">Product Summary</h2>
              <?php
$html= substr($description,0,strrpos(substr($description,0,300)," "));
$final = preg_replace('#<h3>(.*?)</h3>#', '', $html, 1);
echo $final;
?> ...<a href="<?php echo $_SERVER["REQUEST_URI"]; ?>#productdescription">Read More</a>

A example of the text paragraph is below

<br />          <br />  This 11.6” Chromebook is light, portable, rugged, and productive – the ultimate everyday learning tool. It brings Google Classroom, G Suite for Education, and today’s ...<a href="/shop/laptops-tablets/Laptops/lenovo-100e-chromebook-g2-laptop-11-6-celeron-n4020-4gb-32gb-emmc-webcam-wi-fi-no-lan-usb-c-chrome-os#productdescription">Read More</a>

To remove all <br /> tags, you can do something like this:

$html = str_replace('<br />', '', $html);

Will that suffice, or do you only want to remove tags that are at the very beginning of the paragraph?

Currently your existing PHP code uses Regex to remove all <h3> tags regardless of where they are in the string.

Ahh ok, I only need to remove the <h3> tags at the beginning of the paragraph instead of all the <h3> tags regardless of where they are in the string.

I also only need to remove the <br> tags at the beginning of the paragraph, before I removed the <h3> tags, the text looked like the following

<h3><span style="color:#0066cc;"><em><strong>Engineered for education</strong></em></span></h3>
<br>
This 11.6” Chromebook is light, portable, rugged, and productive – the ultimate everyday learning tool. It brings Google Classroom, G Suite for Education, and today’s most popular education apps to students and teachers, making digital education accessible, enjoyable, and manageable. And it does so on a budget any school administration would love.

I just need to remove the <h3> tags and the content in between the <h3 tags

I also need to remove the <br> tag at the beginning as well

OK so my apologies. The , 1 in $final = preg_replace('#<h3>(.*?)</h3>#', '', $html, 1); meant to only perform 1 replacement. Therefore, it removed the <h3> at the beginning of the string.

For the <br /> is there also just one replacement to ever do? What is the heuristic as for where it will appear and how often, in order to use regex to do pattern matching?

That's good regarding the removing of the <h3> tag at the beginning of the string.

There are two <br> tags to be removed I think as the text is below with the <br> tags, there looks to be also a &nbsp; as well

<h2 class="productsummaryheading mb-0">Product Summary</h2>
                        <br>        &nbsp;      <br>    This 11.6” Chromebook is light, portable, rugged, and productive – the ultimate everyday learning tool. It brings Google Classroom, G Suite for Education, and today’s ...<a href="/shop/laptops-tablets/Laptops/lenovo-100e-chromebook-g2-laptop-11-6-celeron-n4020-4gb-32gb-emmc-webcam-wi-fi-no-lan-usb-c-chrome-os#productdescription">Read More</a>

To remove the first <h3> tag and <br> tags from text, you can use regular expressions in programming languages like Python or JavaScript.

commented: !!! -2
commented: might have been useful with some code -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.