Jump to content

Shotern string ignoring html tags


Roaches

Recommended Posts

I was wondering how it to shorten a html formated string without it being split in the middle of a html tag. I can get the function to ignore the html tags when counting how many characters are in the string by just using the strip_tags() function, but once the string is passed the length without html tags it'll split some tags that lie between the break point.

 

For example, if the following string is set to break at 25 characters, it would break the 'a' tag.

This is example text, <a href='#'>link</a> more random text.

 

Is the a regex or some other method to ignore all html tags except <p> when using substr() to shorten the string?

Link to comment
Share on other sites

Even if you could, certain HTML code could cause validation errors on your page.

 

Consider:

<div>This is example text, <a href='#'>link</a> more random text</div>

If you cut the string after the </a>, you would still have an open <div> which you would have to close at some point or you may risk screwing up the layout of your page.  I would say it's better to either strip the tags from the shortened version, or creating a completely separate "preview" that uses a different field in your DB or whatever your source is.

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.