I am trying to remove specific html tags such as <h3>content</h3> and <br> and whitespace from text that is stored in a database and also limit the text to 300 characters then followed by a read more link that scrolls to the full length description text bit further down the page.
I have been putting code together that I got from various forum posts/tutorials from the internet and it’s not pretty but it sort of works, for example it works on this product page:-
https://www.it-doneright.co.uk/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 but on this product page https://www.it-doneright.co.uk/shop/components/Graphics-Cards/asrock-intel-arc-a310-lp-4g-pcie4-4gb-ddr6-hdmi-dp-2000mhz-clock-0db-cooling-low-profile
it leaves ul> but the ul> should not be displayed so the list remains as it is and the ul> should be <ul> but only seen in the source code and not on the page for people to see
Below is the code I currently have, sorry I know it’s not pretty
The second parameter in strip_tags is the things you DONT want the command to eat. It will eat all other tags. Also it’s allowed to have an array as a parameter (7.4+), or null as a parameter (8+).
It also wont eat malformed tags, so the ul> is missing its <, and it isnt a tag anymore.
Sorry what would the code be for that as it goes past my php knowledge of what the code would be for that or is there a example link please that I could try and understand the code and get a example
For keeping <ul> tags but stripping the rest, you can try strip_tags($final, '<ul><li>') instead of your current approach. This allows those specific tags while removing others.
$description = strip_tags($description, '<ul><li>'); // strip all HTML except <ul> and <li>
$description = str_replace(' ', ' ', $description); // replace with actual spaces
$description = preg_replace('~\s+~s', ' ', $description); // replace multiple spaces with single space
$description = trim($description); // strip any spaces at start and end of string
if (strlen($description) > 300) {
$description = substr($description, 0, strrpos($description, ' ', -1 * (strlen($description) - 300))); // limit to 300 characters
}
Though the results of script like these are almost always sub-optimal. It’s better that a human enters a summery for each product, if that’s feasible of course.
The text content and the html tags are imported from a suppliers product feed from their FTP server and stored in the database as it is from the product feed so unsure if it would work by fixing it in the database as the feed is checked every hour or 2 and updated and new products added often from the product feed
Stored in the database by what? Why cant’ THAT process logic to strip out the necessary tags, and leave you with a simple echo when you need it?
You’ll save your server a lot of processing power (if not database space) by filtering the data on the one-time way in, rather than on the multiple-time way out.
The import extension I am using imports the products data from the suppliers product feed and stored in the product database table. I don’t think the import extension I am using in opencart can strip out the necessary tags and it may then change the look of the full description length that is further down the page