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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.