Jump to content

substr escape <b> <u> <i>...


UnknownPlayer

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.