Page 1 of 1

Strip Tags?

Posted: Tue Dec 15, 2009 2:45 pm
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.

Re: Strip Tags?

Posted: Tue Dec 15, 2009 2:55 pm
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.

Re: Strip Tags?

Posted: Tue Dec 15, 2009 3:03 pm
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.

Re: Strip Tags?

Posted: Tue Dec 15, 2009 3:11 pm
by AbraCadaver
Post the actual HTML output of:

Code: Select all

echo $proof;

Re: Strip Tags?

Posted: Tue Dec 15, 2009 3:11 pm
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.

Re: Strip Tags?

Posted: Tue Dec 15, 2009 3:26 pm
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.

Re: Strip Tags?

Posted: Wed Dec 16, 2009 2:39 am
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()

Re: Strip Tags?

Posted: Wed Dec 16, 2009 9:22 am
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?

Re: Strip Tags?

Posted: Wed Dec 16, 2009 11:21 am
by synical21
I have fixed the problem now, Alan G solved it.

Re: Strip Tags?

Posted: Wed Dec 16, 2009 12:50 pm
by AbraCadaver
synical21 wrote:I have fixed the problem now, Alan G solved it.
OK, what was it?

Re: Strip Tags?

Posted: Wed Dec 16, 2009 1:09 pm
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 :)