UnknownPlayer Posted October 5, 2010 Share Posted October 5, 2010 I have front page and page for news, and i wonna to set, on front page to show news but only 100 characters, and on news page to be all text, i done that with substr($news, 0, 100); but i have proble, when i have in $news text like this: Hi everyone. <b>Bold</b> now, subsrt count <b> and </b> like characters, how can i escape that <b> </b>, <u> </u> and other? Quote Link to comment Share on other sites More sharing options...
Rifts Posted October 6, 2010 Share Posted October 6, 2010 substr($news, 0, 105); Quote Link to comment Share on other sites More sharing options...
joel24 Posted October 6, 2010 Share Posted October 6, 2010 strip_tags() Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 6, 2010 Share Posted October 6, 2010 Best I can think of at the moment is to use strip_tags() on $news before using substr(). echo substr(strip_tags($news), 0, 100); Quote Link to comment Share on other sites More sharing options...
UnknownPlayer Posted October 7, 2010 Author Share Posted October 7, 2010 But when i use that function, it will echo me without <b> <u>.... and i wonna to echo that news as it is but just to show 100, hmm i dont know how to understand :S Quote Link to comment Share on other sites More sharing options...
joel24 Posted October 7, 2010 Share Posted October 7, 2010 substr($news, 0, 100) will return the first 100 characters of the $news string, though if you have HTML in that string also, the html is also counted in the 100 characters. you can strip the tags as I stated before and the other options are quite convoluted, such as counting the HTML characters in the string then adding this to the amount of characters to return. To my knowledge there is no inbuilt function to count HTML characters, though substr_count() would count the amount of occurrences of a tag, then you would have to multiply that by the amount of characters in said tag... then do this for every tag and add it to 100 in the substr($news, 0, 100) function. I would just use strip_tags() and show a 100 character teaser, then have a link so the user can view the entire article. Quote Link to comment Share on other sites More sharing options...
UnknownPlayer Posted October 7, 2010 Author Share Posted October 7, 2010 Ok, thanks 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.