Strip Tags?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Strip Tags?

Post by synical21 »

Hello,

I have a small problem bugging me which is not being fixed as quickly as i thought. I have a table which pulls titles out from the database, the problem being these titles can contain <br /> depending on if the user has hit return during the typing of the title. I am trying to display the title without <br />. I was reading at php.net about the strip_tags function so i tried to implement it here:

Code: Select all

 
 echo "<tr>";
 echo "<td width='10%' align='left'>$job_id</td>\n";
 echo "<td width='12%' align='center'>" . substr($userproof_id,0,8) . "</td>\n";
 echo "<td width='60%' align='left'><a href='proofdetails.php?id=$userproof_id&jid=$job_id'>" . strip_tags($proof) . "</a></td>\n";     //-Here is the line i need to strip
 echo "<td width='10%' align='center'><a href='proofprocess.php?id=$userproof_id&jid=$job_id'><img src ='images/tick.png'></img></a></td>\n";
 echo "<td width='5%' align='center'><a href='proofprocessdeny.php?id=$userproof_id&jid=$job_id'><img src ='images/cross.png'></img></td>\n";
 
The function did not work so i was wondering what you think the best approach would be.
thanks.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Strip Tags?

Post by AbraCadaver »

strip_tags() has had problems with self closing tags such as <br />. You might try:

Code: Select all

str_replace('<br />', '', $proof);
If you know that you never want the <br />, then maybe do this before inserting it into the database.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: Strip Tags?

Post by synical21 »

I tried

Code: Select all

. str_replace('<br />', '', $proof) .
which has not worked, i want the tags to stay in the database as they are needed on other pages.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Strip Tags?

Post by AbraCadaver »

Post the actual HTML output of:

Code: Select all

echo $proof;
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Strip Tags?

Post by requinix »

I have yet to understand what "does not work" means because strip_tags most definitely does work.

Also, if someone typed a newline while in a textarea you would have received a newline character not a <br>. I'm thinking somewhere you have a nl2br that doesn't belong.
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: Strip Tags?

Post by synical21 »

It looks like i have a nl2br, either way the user can press return while writing data. Then while that data is echo'd for the title of a row in a table it comes out as every return = <br />.

How do i prevent these <br /> interupting my titles.
jd6th
Forum Newbie
Posts: 7
Joined: Fri Dec 11, 2009 1:50 am

Re: Strip Tags?

Post by jd6th »

before you insert the title in the database, try to replace the "nl2br" and "<br />" with "<br>"

$find = array("nl2br","<br />");
$replace = array("<br>","<br>");

$title=str_replace($find,$replace,$_POST['title']);

you have the variable $title to be inserted in the database.... now you can use the strip_tags()
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: Strip Tags?

Post by synical21 »

Ok i couldnt get your piece of code to work but i have made the same result happen using

Code: Select all

return str_replace("xyxy", "<br>\n", $text);
Then i added the stip tags function back like in hte original post. Still this has not fixed the error now i get this:
zzzzzzzzzz1<br> zzzzzzzzzzzz2
So replacing the <br /> has worked but the stip tags still does not pick up the <br>. Any more suggestions?
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: Strip Tags?

Post by synical21 »

I have fixed the problem now, Alan G solved it.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Strip Tags?

Post by AbraCadaver »

synical21 wrote:I have fixed the problem now, Alan G solved it.
OK, what was it?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: Strip Tags?

Post by synical21 »

Basically when the data was being inserted into the DB ensure /n was being used. For some reason <br> was being inserted into the DB for each new line. Then when i want the page to use line breaks i would use the nl2br function

EG

Code: Select all

echo nl2br($row['proof']);
So in the title of a table i wouldnt use nl2br :)
Post Reply