Omzy Posted July 20, 2009 Share Posted July 20, 2009 Basically I'm using the strip_tags() function to (obviously) strip out the HTML tags from a string. The new string is then used to form a <meta description> tag. What happens is when I view the source of the page (in the browser) I get the stripped string but there are loads of line breaks throughout the string. It seems like these line breaks are where the HTML tags have been stripped out. How can I get rid of these line breaks? I have tried the trim() function but that doesn't seem to work. I have also tried str_replace(" ", "", $string)... Link to comment https://forums.phpfreaks.com/topic/166612-strip_tags-and-line-breaks/ Share on other sites More sharing options...
trq Posted July 20, 2009 Share Posted July 20, 2009 Seems odd. Try str_replace("\n", "", $string); Link to comment https://forums.phpfreaks.com/topic/166612-strip_tags-and-line-breaks/#findComment-878554 Share on other sites More sharing options...
Omzy Posted July 20, 2009 Author Share Posted July 20, 2009 That doesn't seem to work either! Link to comment https://forums.phpfreaks.com/topic/166612-strip_tags-and-line-breaks/#findComment-878556 Share on other sites More sharing options...
thebadbad Posted July 20, 2009 Share Posted July 20, 2009 Please show us a var_dump() (from "view source") of a simple sample string before and after it has been run through strip_tags(). Link to comment https://forums.phpfreaks.com/topic/166612-strip_tags-and-line-breaks/#findComment-878563 Share on other sites More sharing options...
Daniel0 Posted July 20, 2009 Share Posted July 20, 2009 Maybe try this? $str = preg_replace('#\s{2,}#', '', $str); Link to comment https://forums.phpfreaks.com/topic/166612-strip_tags-and-line-breaks/#findComment-878582 Share on other sites More sharing options...
Omzy Posted July 20, 2009 Author Share Posted July 20, 2009 Cheers Daniel0, that has worked! But not quite the finished article yet... how can I stop it from outputting in the source? Link to comment https://forums.phpfreaks.com/topic/166612-strip_tags-and-line-breaks/#findComment-878603 Share on other sites More sharing options...
Daniel0 Posted July 20, 2009 Share Posted July 20, 2009 You'll have to show us some code before we can answer that. Link to comment https://forums.phpfreaks.com/topic/166612-strip_tags-and-line-breaks/#findComment-878608 Share on other sites More sharing options...
Omzy Posted July 20, 2009 Author Share Posted July 20, 2009 Well basically, as well outputting extra line breaks it was also outputting in the html source. So after applying your fix above it got rid of the extra line breaks so I now get it all in-line as it should be: <meta name="Description" content=" Technical Specifications: Platform: DS BBFC Rating: Description: 100 Classic Book Collection turns your Nintendo DS into.. " /> Can your preg_match code be extended to take out all the ? Link to comment https://forums.phpfreaks.com/topic/166612-strip_tags-and-line-breaks/#findComment-878613 Share on other sites More sharing options...
Daniel0 Posted July 20, 2009 Share Posted July 20, 2009 You must somewhere be calling htmlentities(). Seeing as you strip all HTML, that is redundant. Link to comment https://forums.phpfreaks.com/topic/166612-strip_tags-and-line-breaks/#findComment-878621 Share on other sites More sharing options...
Omzy Posted July 20, 2009 Author Share Posted July 20, 2009 No it's definitely not calling htmlentities or any other method. Not as far as I can see anyway... This is just really strange, normally strip_tags() and trim() is all you need to get rid of line breaks, spaces and html tags. But I've already had to use a preg_replace function to get rid of the line breaks and will probably now need to use some other function to get rid of the ! Link to comment https://forums.phpfreaks.com/topic/166612-strip_tags-and-line-breaks/#findComment-878626 Share on other sites More sharing options...
Omzy Posted July 20, 2009 Author Share Posted July 20, 2009 Just done this: preg_replace("#\s{2,}| #", "", $str); Everything sorted now :-) Link to comment https://forums.phpfreaks.com/topic/166612-strip_tags-and-line-breaks/#findComment-878635 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.