ianhaney10 Posted June 20 Share Posted June 20 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> Quote Link to comment https://forums.phpfreaks.com/topic/321868-remove-first-h3-tag-and-br-tags-in-text/ Share on other sites More sharing options...
requinix Posted June 20 Share Posted June 20 Perhaps you'd be interested in a function that can strip HTML tags except for maybe some that you might want to keep? Quote Link to comment https://forums.phpfreaks.com/topic/321868-remove-first-h3-tag-and-br-tags-in-text/#findComment-1628420 Share on other sites More sharing options...
gizmola Posted June 20 Share Posted June 20 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> Quote Link to comment https://forums.phpfreaks.com/topic/321868-remove-first-h3-tag-and-br-tags-in-text/#findComment-1628427 Share on other sites More sharing options...
ianhaney10 Posted June 20 Author Share Posted June 20 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 Quote Link to comment https://forums.phpfreaks.com/topic/321868-remove-first-h3-tag-and-br-tags-in-text/#findComment-1628437 Share on other sites More sharing options...
gizmola Posted June 20 Share Posted June 20 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. Quote Link to comment https://forums.phpfreaks.com/topic/321868-remove-first-h3-tag-and-br-tags-in-text/#findComment-1628439 Share on other sites More sharing options...
ianhaney10 Posted June 20 Author Share Posted June 20 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> <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/ code. Hopefully that helps and explains it bit better Quote Link to comment https://forums.phpfreaks.com/topic/321868-remove-first-h3-tag-and-br-tags-in-text/#findComment-1628441 Share on other sites More sharing options...
gizmola Posted June 29 Share Posted June 29 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[] = '# #'; $replacements[] = ' '; $patterns[] = '#<br \/>\s+<br \/>#'; $replacements[] = ''; $final = preg_replace($patterns, $replacements, $html); Quote Link to comment https://forums.phpfreaks.com/topic/321868-remove-first-h3-tag-and-br-tags-in-text/#findComment-1629001 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.