Jump to content

Limiting an HTML article to a # of characters.


munsiem

Recommended Posts

I am relatively new here, so I thought I would go ahead and get started with my first post.  I am currently writing a script that takes a block of text (with html statements included) and limiting it to a # of characters.

 

Ex:

 

"My name is <b>Mike</b> and I am here to program"

 

If I limited this to 12 characters, it should display:

 

"My name is "...

 

My script is working pretty decently right now, but I was wondering if anyone had a better idea.

 

This is the basic layout for the code >>

 

Step 1. "Create two strings"

 

- One with the HTML statements included

- The other with just the text and no HTML tags

 

Step 2. "Take all the HTML tags and place them into an array"

 

With doing so, I also keep track of the location within the text of where the HTML tag was stored.

 

Step 3. "Cut off the article by # of characters"

 

I then take the article and chop it to # of characters.  I wrote a routine that rounds to the nearest word.

 

Ex: "my name is mike"

I accidentally submitted without finishing the article! Lol, man I am a noob...

 

Anyways, the next portion of this is:

 

...

 

Ex: "my name is mike"

                ^^ Cutoff point is 12 chars

 

Would be:-> "my name is"...

 

Step 4. "Place HTML Tags Back In"

 

I then created a routine where I placed my HTML tags back in by their location in the original string.  I also keep a counter of the length of the article with the # of chars limited.  This is so every HTMl tag within the original text can be placed back into it's position.

 

Step 4. "Close HTML Tags and Display"

 

Finally I close any tag that was left open so that the formatting of my text afterwards will not be changed.

 

 

??? Any ideas for an easier way?  ???

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.