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? Link to comment https://forums.phpfreaks.com/topic/215247-substr-escape/ Share on other sites More sharing options...
Rifts Posted October 6, 2010 Share Posted October 6, 2010 substr($news, 0, 105); Link to comment https://forums.phpfreaks.com/topic/215247-substr-escape/#findComment-1119473 Share on other sites More sharing options...
joel24 Posted October 6, 2010 Share Posted October 6, 2010 strip_tags() Link to comment https://forums.phpfreaks.com/topic/215247-substr-escape/#findComment-1119474 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); Link to comment https://forums.phpfreaks.com/topic/215247-substr-escape/#findComment-1119475 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 Link to comment https://forums.phpfreaks.com/topic/215247-substr-escape/#findComment-1119715 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. Link to comment https://forums.phpfreaks.com/topic/215247-substr-escape/#findComment-1119716 Share on other sites More sharing options...
UnknownPlayer Posted October 7, 2010 Author Share Posted October 7, 2010 Ok, thanks Link to comment https://forums.phpfreaks.com/topic/215247-substr-escape/#findComment-1119974 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.