News:

Want to get involved in developing SMF? Why not lend a hand on our GitHub!

Main Menu

Cutting of HTML strings in preview without a complete mess?

Started by Ncen, June 07, 2011, 03:18:06 PM

Previous topic - Next topic

Ncen

Hi all,

I've been researching this for a couple of days. I have a script which uses shorten_subject and in this case it cuts of the message, 'preview1' => shorten_subject($row['firstBody'],350), (of course there's a lot of other text) and it displays the text with something like ', $topic['first_post']['preview1'], '

I also use custom bbc and my members use it to style the preview. So they might have a code that looks like this (in html/source):
<div style="color: #900; font-family: georgia;">Here is a text.
<img="adress.jpg" width="50%">
Lots of other text <b>here as well</b><tt>~Whatever!</div>

You can imagine what happens to my theme if it cuts of before the final </div>, or in the middle of the image url, etc. Not nice. :( So i changed the code and i'm using another one to display the whole message using ', $topic['first_post']['body'], ' So far, so good. It now displays the whole message.

I'm looking into another method, still using ', $topic['first_post']['body'], ' but cutting it of a bit. I've researched and found methods to strip the text of html, but that would not work. I need the html to display but also include end tags.

Then i found this two pages:
http://prajwalaa.wordpress.com/2009/04/12/cut-html-string-by-keeping-html-tags-as-it-is/
http://www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags/
(i hope it's okay for me to post the links here)

And then i can't get it too work. I'm not sure who to input my ', $topic['first_post']['body'], ' into it, so that it could cut off the text after for example 350 characters, but still be able to close the html tags.
I've been looking into the first page the most because there's an example, unfortunately i still can't get any of it to work. The second i don't even know how to get it to print out the post.
Is it not possible, or does anyone have an idea on how to accomplish it? And do I have too use the javascript version (in first page) since my code includes " which seems to break the phpversion (in first page)?

Edit: I should add that my members sometimes have unnecessary codes like too many <p> and <tt> which is not cut of correctly, even in their message. I was thinking that this would also work out that.
And: I need for it to also work with div-tags, and be able to accept UTF8 special characters.


Edit 2: I think the reason i can't get it to work (php version, first page) is that my code involved <div>tags and some tags are not closed correctly to begin with.

Thanks,
Ncen.

Using version: 1.1.13

Advertisement: