PDA

View Full Version : which php function should i use?



bwh2
June 17th, 2005, 01:58 AM
i have users entering news articles into a db. when the articles are printed back out, i need a return to be replaced with a </p><p> to end the last paragraph and being a new one.

so what php function should i used to locate a return and insert </p><p>? basically i want it to function just like a post in this forum in that sense.

beanSoldier
June 17th, 2005, 02:32 AM
$replace2 = array("\r\n", '"');
$replaceWith2 = array("<br />", "&quot;");
$theText = str_replace($replace2, $replaceWith2, $theText);

teiz77
June 20th, 2005, 04:11 AM
can be done easier... just use nl2br($string) http://nl3.php.net/manual/en/function.nl2br.php It autmatically does the job for you.

bwh2
June 20th, 2005, 10:09 AM
thanks beansoldier and teiz. i'm think i'm going to use beansoldier's suggestion b/c i can use it to prevent the user from entering html (unless someone knows a better way to do that also?)

teiz77
June 20th, 2005, 10:18 AM
thanks beansoldier and teiz. i'm think i'm going to use beansoldier's suggestion b/c i can use it to prevent the user from entering html (unless someone knows a better way to do that also?)

Beansoldiers code is not safe enough, it only filters double quotes. Users can still post html code. You should stick to the standard PHP functions to do this.
The main function for this is htmlentitites (http://nl3.php.net/manual/en/function.htmlentities.php)

some examples:


<?php
$str = "A 'quote' is
<b>bold</b>";

$str = nl2br(htmlentities($str));

echo $str;

?>
You have to do nl2br last... otherwise this htmlcode also gets converted.

*edited to add code coloring

bwh2
June 20th, 2005, 11:07 AM
i know the old code wouldn't be sufficient to get rid of html; i was planning on replacing common html strings with quotation marks. the reason why i thought i'd use this (and not htmlentities) is because i want the html to just disappear. that is, when a user types "<b>some text</b>", i want it to output just "some text", not "&lt;b&gt;bold&lt;/b&gt".

teiz77
June 20th, 2005, 11:11 AM
hehe... php can also do that... use the strip_tags()...

bwh2
June 20th, 2005, 09:53 PM
excellent. i'll check it out.

JustJeff
June 21st, 2005, 02:14 PM
Even strip_tags isn't that safe - you can inject a LOT of code into <a>, <img>, and <p> tags.

teiz77
June 22nd, 2005, 10:11 AM
Even strip_tags isn't that safe - you can inject a LOT of code into <a>, <img>, and <p> tags.

<a>, <img>, and <p> are tags... this function strips all tags, also these.