Jump to content

remove first h3 tag and br tags in text


Recommended Posts

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>
Link to comment
Share on other sites

requinix made an excellent suggestion, that could be better if you want to have a whitelist/blacklist of tags.

However, along the lines of what you are already doing, you could add an additional replacement pattern. 

This might need to be improved if there is any possibility of variation in the br tags, but if they are all standard and self closing as in your example text, one advantage of this is that it will find a br, followed by any amount of whitespace, followed by a 2nd br. 

<br \/>\s+<br \/>

 

One of the nice things about preg_replace, when you need to do multiple replacements, is that you can set them up using 2 arrays and run them in one call.

<h2 class="productsummaryheading mb-0">Product Summary</h2>
<?php
$html= substr($description,0,strrpos(substr($description,0,300)," "));

$patterns[] = '#<h3>(.*?)</h3>#';
$replacements[] = '';

$patterns[] = '#<br \/>\s+<br \/>#';
$replacements[] = '';

$final = preg_replace($patterns, $replacements, $html, 1);
echo $final;
?> 
...<a href="<?php echo $_SERVER["REQUEST_URI"]; ?>#productdescription">Read More</a>

 

Link to comment
Share on other sites

2 hours ago, gizmola said:

requinix made an excellent suggestion, that could be better if you want to have a whitelist/blacklist of tags.

However, along the lines of what you are already doing, you could add an additional replacement pattern. 

This might need to be improved if there is any possibility of variation in the br tags, but if they are all standard and self closing as in your example text, one advantage of this is that it will find a br, followed by any amount of whitespace, followed by a 2nd br. 

<br \/>\s+<br \/>

 

One of the nice things about preg_replace, when you need to do multiple replacements, is that you can set them up using 2 arrays and run them in one call.

<h2 class="productsummaryheading mb-0">Product Summary</h2>
<?php
$html= substr($description,0,strrpos(substr($description,0,300)," "));

$patterns[] = '#<h3>(.*?)</h3>#';
$replacements[] = '';

$patterns[] = '#<br \/>\s+<br \/>#';
$replacements[] = '';

$final = preg_replace($patterns, $replacements, $html, 1);
echo $final;
?> 
...<a href="<?php echo $_SERVER["REQUEST_URI"]; ?>#productdescription">Read More</a>

 

Thank you so much for the reply, I did try functions and they don't seem to work properly in the .tpl file.

I tried your code above but still got the br tags showing still unfortunately in the text at the beginning

Link to comment
Share on other sites

I would need to have some sample text in order to determine what the issue might be. 

Do you understand what the regex is doing?  It assumes there must be 2 br's with space between them.  If it doesn't match that exactly it won't remove things.  If there are other br's that need to be removed that can be handled, but what is in the actual database entries is important to understand.

 

One other thing.. you are currently passing a limit of 1 match/replacement as the parameter.  So if there are multiple occurrences, it will only take care of 1.  I'm not sure why you are limiting it that way, but it could also explain a potential issue.

 

Link to comment
Share on other sites

20 minutes ago, gizmola said:

I would need to have some sample text in order to determine what the issue might be. 

Do you understand what the regex is doing?  It assumes there must be 2 br's with space between them.  If it doesn't match that exactly it won't remove things.  If there are other br's that need to be removed that can be handled, but what is in the actual database entries is important to understand.

 

One other thing.. you are currently passing a limit of 1 match/replacement as the parameter.  So if there are multiple occurrences, it will only take care of 1.  I'm not sure why you are limiting it that way, but it could also explain a potential issue.

 

Ahh ok, below is the same text with the html tags

<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>

The <h3 tags are already removed, just leaves the other tags I want to remove such as the two <br> tags and the whitespace/&nbsp; code. Hopefully that helps and explains it bit better

Link to comment
Share on other sites

  • 2 weeks later...

The reason it didn't work is because you have an html entity for a space in there.  You could convert that to a space 1st.

$patterns[] = '#<h3>(.*?)</h3>#';
$replacements[] = '';

$patterns[] = '#&nbsp;#';
$replacements[] = ' ';

$patterns[] = '#<br \/>\s+<br \/>#';
$replacements[] = '';

$final = preg_replace($patterns, $replacements, $html);

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.