Jump to content

Recommended Posts

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>";
} 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/223981-stripping-html-tags/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/223981-stripping-html-tags/#findComment-1157476
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/223981-stripping-html-tags/#findComment-1157569
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/223981-stripping-html-tags/#findComment-1157578
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.