tgavin Posted February 9, 2007 Share Posted February 9, 2007 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. Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/ Share on other sites More sharing options...
artacus Posted February 9, 2007 Share Posted February 9, 2007 Why truncate the text from php? Maybe you'd be better off handling it on the client side by not letting any text be added once the counter hits 0. That way they can see if it will be messed up. Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180281 Share on other sites More sharing options...
tgavin Posted February 9, 2007 Author Share Posted February 9, 2007 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. Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180310 Share on other sites More sharing options...
artacus Posted February 9, 2007 Share Posted February 9, 2007 I'm confused. Are you going to remove the HTML? If that's what you're doing use strip_tags(); Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180316 Share on other sites More sharing options...
tgavin Posted February 9, 2007 Author Share Posted February 9, 2007 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 Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180350 Share on other sites More sharing options...
Jessica Posted February 9, 2007 Share Posted February 9, 2007 $text = $_POST['text']; $noHTML = strip_tags($text); print strlen($noHTML); Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180353 Share on other sites More sharing options...
tgavin Posted February 9, 2007 Author Share Posted February 9, 2007 This stupid forum isn't letting me post the code! Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180360 Share on other sites More sharing options...
Jessica Posted February 9, 2007 Share Posted February 9, 2007 Hit reply, paste your code, highlight it and press the # button in the editor. Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180361 Share on other sites More sharing options...
tgavin Posted February 9, 2007 Author Share Posted February 9, 2007 [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 Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180366 Share on other sites More sharing options...
Jessica Posted February 9, 2007 Share Posted February 9, 2007 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. Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180368 Share on other sites More sharing options...
tgavin Posted February 9, 2007 Author Share Posted February 9, 2007 It's still not showing up. Look at it <a href="http://www.ginteractive.com/code.html">here</a> Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180371 Share on other sites More sharing options...
Jessica Posted February 9, 2007 Share Posted February 9, 2007 Post your new code which uses the technique I showed you. Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180375 Share on other sites More sharing options...
tgavin Posted February 9, 2007 Author Share Posted February 9, 2007 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? Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180377 Share on other sites More sharing options...
Jessica Posted February 9, 2007 Share Posted February 9, 2007 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. Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180379 Share on other sites More sharing options...
tgavin Posted February 9, 2007 Author Share Posted February 9, 2007 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! Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180384 Share on other sites More sharing options...
tgavin Posted February 9, 2007 Author Share Posted February 9, 2007 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);" '; Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180386 Share on other sites More sharing options...
Jessica Posted February 9, 2007 Share Posted February 9, 2007 I don't know what the appropriate js is to remove tags. I'd use ajax to power the character counter. Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180389 Share on other sites More sharing options...
artacus Posted February 9, 2007 Share Posted February 9, 2007 Oh, and is "embiggens" really a word? Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180396 Share on other sites More sharing options...
tgavin Posted February 9, 2007 Author Share Posted February 9, 2007 I don't know what the appropriate js is to remove tags. I'd use ajax to power the character counter. I'll look into it. Thanks again. Oh, and is "embiggens" really a word? It's a perfectly cromulent word. Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180398 Share on other sites More sharing options...
artacus Posted February 9, 2007 Share Posted February 9, 2007 It's a perfectly cromulent word. Wow. Its not often that some one sends me to the dictionary. I still don't by the embiggens one though. Link to comment https://forums.phpfreaks.com/topic/37693-insert-or-echo-text-and-ignore-scripthtml-tags/#findComment-180404 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.