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
https://forums.phpfreaks.com/topic/92536-shotern-string-ignoring-html-tags/
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.

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.