Mundane Posted January 10, 2011 Share Posted January 10, 2011 Hi, this is probably really simple for the majority here, but it's catching me out. I want to know what I need to add to the code below to strip <> HTML tags from the field post_title. Hope somebody can help. Thanks in advance. <?php mysql_connect("********", "********", "********") or die(mysql_error()); mysql_select_db("website_news") or die(mysql_error()); $data = mysql_query("SELECT * FROM news_posts ORDER BY post_date DESC LIMIT 0, 5") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { Print "<font color='black' face='arial'><li><a href='/website/news/?p=".$info['ID'] . "'>"; if ( strlen($info['post_title']) > 60) { $PostTitle = substr($info['post_title'], 0, 60); print $PostTitle. "..."; } else { print $info['post_title']; } Print "</a> <font color=#666666><i> - posted on "; $date = date('j M \'y', strtotime($info['post_date'])); Print $date; Print "</i>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/223981-stripping-html-tags/ Share on other sites More sharing options...
jdavidbakr Posted January 10, 2011 Share Posted January 10, 2011 It'd be a lot easier to store the fields without the HTML and add them when you display them That said, it can be done using regex, but gets scary if you don't have control over the data. This regex will strip anything that starts with "<" and ends with ">": $result = preg_replace("/<[^>]*>/","",$data); but will break if you have something like <p>I just learned that 2 < 3!</p> as your string. There's probably a more robust HTML removing regex, someone may even be able to give it to you ... but I'm going to stick with my original answer which is to avoid putting the HTML into the field in the first place. Quote Link to comment https://forums.phpfreaks.com/topic/223981-stripping-html-tags/#findComment-1157476 Share on other sites More sharing options...
ignace Posted January 10, 2011 Share Posted January 10, 2011 strip_tags (it also has an optional list to allow certain tags) Quote Link to comment https://forums.phpfreaks.com/topic/223981-stripping-html-tags/#findComment-1157478 Share on other sites More sharing options...
Mundane Posted January 10, 2011 Author Share Posted January 10, 2011 Thanks for your reply guys. jdavidbakr, the data is added to the database by third party software which I can't edit. I'm trying to remove the HTML tags in case a malicious user was to insert some when submitting the data (as I said, I can't stop it being submitted). ignace, where exactly do I insert it in my code, the strlen is throwing me off. Quote Link to comment https://forums.phpfreaks.com/topic/223981-stripping-html-tags/#findComment-1157569 Share on other sites More sharing options...
jdavidbakr Posted January 10, 2011 Share Posted January 10, 2011 that's awesome, ignace. I love it when I discover a new useful function :-) Mundane, probably the easiest thing to do is to store your row in another variable and then operate on that: $title = strip_tags($info['post_title']); Then replace any usage of $info['post_title'] with $title. Quote Link to comment https://forums.phpfreaks.com/topic/223981-stripping-html-tags/#findComment-1157578 Share on other sites More sharing options...
Mundane Posted January 10, 2011 Author Share Posted January 10, 2011 Thanks guys, got it. Quote Link to comment https://forums.phpfreaks.com/topic/223981-stripping-html-tags/#findComment-1157592 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.