cooldude832 Posted August 6, 2008 Share Posted August 6, 2008 I'm trying to escape html tags so I can put them in a textarea with out having a </textarea> stop the text area example <?php $var = "<P><textarea name\"var1\">Blah</textarea>"; echo "<textarea name=\"update\">".$var."</textarea>"; ?> But because $var has a </textarea> in it it breaks the textarea there instead of later Link to comment https://forums.phpfreaks.com/topic/118400-escaping-html-tags-for-a-textarea/ Share on other sites More sharing options...
cooldude832 Posted August 6, 2008 Author Share Posted August 6, 2008 beyond doing $var = str_replace("</textarea>","[/textarea]",$var); Link to comment https://forums.phpfreaks.com/topic/118400-escaping-html-tags-for-a-textarea/#findComment-609353 Share on other sites More sharing options...
Third_Degree Posted August 6, 2008 Share Posted August 6, 2008 Functions you could use: strip_tags htmlentities (with ENT_QUOTES parameter) <- MY RECOMMENDATION str_replace/ereg_replace/preg_replace Link to comment https://forums.phpfreaks.com/topic/118400-escaping-html-tags-for-a-textarea/#findComment-609354 Share on other sites More sharing options...
teng84 Posted August 6, 2008 Share Posted August 6, 2008 ignore ??? Link to comment https://forums.phpfreaks.com/topic/118400-escaping-html-tags-for-a-textarea/#findComment-609356 Share on other sites More sharing options...
Third_Degree Posted August 6, 2008 Share Posted August 6, 2008 Oh, I see what you mean. I guess not then... Link to comment https://forums.phpfreaks.com/topic/118400-escaping-html-tags-for-a-textarea/#findComment-609357 Share on other sites More sharing options...
Naez Posted August 6, 2008 Share Posted August 6, 2008 <?php function textarea ($str) { $open = '<textarea>'; $close = '</textarea>'; // Count the opening tags preg_match_all ('/\[textarea\]/i', $str, $matches); $opentags = count($matches['0']); // Count the close tags preg_match_all ('/\[\/textarea\]/i', $str, $matches); $closetags = count($matches['0']); // reiterate through loop to close all the open tags, there's probably a more elegant way of doing this $unclosed = $opentags - $closetags; for ($i = 0; $i < $unclosed; $i++) { $str .= $close; } // Do replacement $str = str_replace ('[textarea]', $open, $str); $str = str_replace ('[/textarea]', $close, $str); return $str; } ?> Link to comment https://forums.phpfreaks.com/topic/118400-escaping-html-tags-for-a-textarea/#findComment-609378 Share on other sites More sharing options...
Third_Degree Posted August 6, 2008 Share Posted August 6, 2008 <?php function textarea ($str) { $open = '<textarea>'; $close = '</textarea>'; // Count the opening tags preg_match_all ('/\[textarea\]/i', $str, $matches); $opentags = count($matches['0']); // Count the close tags preg_match_all ('/\[\/textarea\]/i', $str, $matches); $closetags = count($matches['0']); // reiterate through loop to close all the open tags, there's probably a more elegant way of doing this $unclosed = $opentags - $closetags; for ($i = 0; $i < $unclosed; $i++) { $str .= $close; } // Do replacement $str = str_replace ('[textarea]', $open, $str); $str = str_replace ('[/textarea]', $close, $str); return $str; } ?> You could still end up with <textarea>stuff blah</textarea>hahahaahahahah<textarea></textarea></textarea> Link to comment https://forums.phpfreaks.com/topic/118400-escaping-html-tags-for-a-textarea/#findComment-609385 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.