Jump to content


Photo

How To Autodetect Paragraph Breaks


  • Please log in to reply
10 replies to this topic

#1 garethppls

garethppls
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 28 June 2006 - 04:24 PM

I have a form but I want to disable HTML. the problem with this is the form doesn't recognise paragraph breaks. any way of doing this like on this forum.
thanks,


#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 28 June 2006 - 04:28 PM

is your answer in one of these 3?

<?php
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
echo "\n";

// Allow <p>
echo strip_tags($text, '<p>');
?>

The above example will output:

Test paragraph. Other text
<p>Test paragraph.</p> Other text

<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // &lt;a href='test'&gt;Test&lt;/a&gt;
?>


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

// Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str);

// Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str, ENT_QUOTES);
?>

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 June 2006 - 06:04 PM

You'll want to lookinto nl2br function ie:
<?php

$str = "this text\nhas line\nbreaks in\n it!";

// convert carriage returns into line breaks (<br />)
echo nl2br($str);

?>

Also if you want to only allow paragraph tags you can use a function called [a href=\"http://www.php.net/strip-tags\" target=\"_blank\"]strip_tags[/a]. For example:
<?php

$str = "this text<p>is only <b>allowed</b></p>paragraphs!";

// strip all tags from $str but keep < p></p>!
echo strip_tags($str, '<p>');

?>

Hope that helps.


#4 garethppls

garethppls
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 28 June 2006 - 08:56 PM

no I want it to detect paragraph breaks in a form without putting <p> in , just like this forum as its a forum script i'm making

#5 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 28 June 2006 - 09:00 PM

User eregi or eregi_replace to do something like...

eregi("\n\r", $string);
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#6 garethppls

garethppls
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 28 June 2006 - 09:52 PM

what if \n or \r arent contained in it?

#7 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 28 June 2006 - 10:15 PM

One thing I've noticed is that with <input type="text"> tags in HTML when they are submitted to a database (like MySQL) it will read the carriage returns and store them properly.

Using echo nl2br($string); works fine when outputting what was in the database and putting in the paragraphs properly. Seems to be fine for windows/macs/un*x type plateforms.

*edit ok that is cool, the forum read my input tag and displayed it hehe

#8 garethppls

garethppls
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 29 June 2006 - 07:43 AM

thanks Sharkbait will try that now

#9 garethppls

garethppls
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 29 June 2006 - 07:37 PM

yep that worked thanks sharkbait. I used nl2br before inserting it to the MYSQL but its all good :)

#10 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 29 June 2006 - 08:10 PM

Isn't that what I suggested in my post above? Prehaps I didnt explain it very well.

#11 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 29 June 2006 - 10:44 PM

[!--quoteo(post=389422:date=Jun 29 2006, 01:10 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 29 2006, 01:10 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Isn't that what I suggested in my post above? Prehaps I didnt explain it very well.
[/quote]


You did, I was just agreeing with what you said :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users