Jump to content

insert (or echo) text and ignore script/html tags?


tgavin

Recommended Posts

I have a block of text that has HTML in it, with links, etc. This block of text will need to be truncated after X amount a characters. I have all of that taken care of. My problem comes into play when the block of text is truncated right in the middle of a link! The link ends up getting broken in half, and then it won't work.

 

The input for the text is a textarea with a javascript counter, that counts down the remaining characters until 0. I also have it set up in the form processor for php to truncate the text - to be on the safe side. So, I'm looking for a way to be able to insert characters and have it ignore any html tags - if that's even possible.

 

I'm not really sure how to correct this.

I'm doing both because if the client has javascript disabled, php will truncate instead.

 

Once the counter reaches zero, no more text can be inserted anyway - that's whole point of having a character counter :)

 

The problem is that the user will enter the text - including html - and the counter will reach zero while counting the html code. So, if the counter will only allow 90 characters, I still need to have 90 characters *without the code*.

 

I could have presented this in a javascript forum, but was curious if there might be a php solution as well.

No, I want the html to stay.

 

Let's say that this textarea will only accept 25 characters. The following sentence is 21 characters, so it will be accepted:

visit my website here

 

Now, if I insert a link into that:

visit my website <a href="http://www.mywebsite.com">here

[pre]No, I want the html to stay.

 

Let's say that this textarea will only accept 25 characters. The following sentence is 21 characters, so it will be accepted:

visit my website here

 

[pre]Now, if I insert a link into that:[/pre]

visit my website <a href="http://www.mywebsite.com">here

Did you even read what I wrote for you?

That will check how long the text is without the HTML in it.

Then you can still use the text which HAD HTML and store that.

I didn't read it until now. I was fighting with this forum not accepting my example.

 

In the code you posted, isn't that stripping the code from the string and then counting the string length?

EXACTLY. That's what you want! You want to know how many characters are there, excluding HTML. SO STRIP THE HTML AND COUNT IT.

You'd need to make the same change to your JS counter.

 

If the problem is that your field is too small, then you need to communicate that to the users, that they can't insert more than X chars INCLUDING HTML.

EXACTLY. That's what you want! You want to know how many characters are there, excluding HTML. SO STRIP THE HTML AND COUNT IT.

You'd need to make the same change to your JS counter.

I see what you're saying. Pretty clever. :)

(I'm in my 16th hour and not too sharp right now...)

 

Thanks for your help!

You wouldn't happen to know javascript would you ;)

 

function limitText(limitField, limitCount, limitNum, e) {
limitField.value = limitField.value.substring(0, limitNum);	// ensures length in case of Copy&Paste
var obj = document.getElementById(limitCount);
obj.innerHTML = limitNum - limitField.value.length;
var goodkeys = new Array(8, 9, 33, 34, 35, 36, 37, 38, 39, 40, 46);
for (i=0; i < goodkeys.length; i++) { if (e.keyCode == goodkeys[i]) return true; }	// allow delete, backspace, arrows, etc.
if (limitField.value.length >= limitField.getAttribute("maxlength")) return false;	// if too many chars, just ignore key event
return true;
}

echo 'onkeydown="limitText(this,\''.$name.''.'_tfcount\','.$head_limit.',event);" onkeyup="limitText(this,\''.$name.''.'_tfcount\','.$head_limit.',event);" ';

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.