tHud Posted September 28, 2011 Share Posted September 28, 2011 Hello $row[longdescription] contains a description of a product (complete with HTML mark-up). I would like to display the first 250 characters in the results of a search. I would also like to remove any images that may crop up. This is what I have so far... echo strip_tags(html_entity_decode(substr(preg_replace('/<img[^>]*>/Ui', '', $row[longdescription]), 0, 250))); The thing is (as you guys can probably already see), it also counts the HTML mark-up as characters and I get very different outputs (especially if an image occurs). I'd appreciate if you could advise me as to the correct way of doing this. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/248013-extracting-a-paragraph-minus-the-html/ Share on other sites More sharing options...
Buddski Posted September 28, 2011 Share Posted September 28, 2011 Shouldnt echo substr(strip_tags($row['longdescription']), 0, 250); Do what you need? Quote Link to comment https://forums.phpfreaks.com/topic/248013-extracting-a-paragraph-minus-the-html/#findComment-1273462 Share on other sites More sharing options...
tHud Posted September 28, 2011 Author Share Posted September 28, 2011 Hi Unfortunately not. This is what the output looks like... <div style="padding-bottom: 10px"> <strong><span class="bodyTitle themeColor">Seamless information workflow and dahdah monitoring </span></strong><br /> <br /> dah dah dah Quote Link to comment https://forums.phpfreaks.com/topic/248013-extracting-a-paragraph-minus-the-html/#findComment-1273464 Share on other sites More sharing options...
Buddski Posted September 28, 2011 Share Posted September 28, 2011 Oh I see, perhaps this echo substr(strip_tags(html_entity_decode($row['longdescription'])), 0, 250); Quote Link to comment https://forums.phpfreaks.com/topic/248013-extracting-a-paragraph-minus-the-html/#findComment-1273466 Share on other sites More sharing options...
tHud Posted September 28, 2011 Author Share Posted September 28, 2011 oh yeah - that hit the spot! Thank you Buddski!!! Can I be cheeky now and ask if it possible to end the string at the end of a word - and not in mid word as I am currently doing? Thank you :) Quote Link to comment https://forums.phpfreaks.com/topic/248013-extracting-a-paragraph-minus-the-html/#findComment-1273471 Share on other sites More sharing options...
Buddski Posted September 28, 2011 Share Posted September 28, 2011 Yep, after you have got your max 250 characters, find the last occurrence of ' ' and end the string before that last ' ' Quote Link to comment https://forums.phpfreaks.com/topic/248013-extracting-a-paragraph-minus-the-html/#findComment-1273476 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.