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)... Quote Link to comment Share on other sites More sharing options...
trq Posted July 20, 2009 Share Posted July 20, 2009 Seems odd. Try str_replace("\n", "", $string); Quote Link to comment 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! Quote Link to comment 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(). Quote Link to comment 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); Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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 ? Quote Link to comment 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. Quote Link to comment 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 ! Quote Link to comment 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 :-) Quote Link to comment 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.