Support NATA 2014

Php: Counting Line Breaks

  • 10,234 Views
  • 17 Replies
New Topic Respond to this Topic
Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Php: Counting Line Breaks Jan. 9th, 2006 @ 07:18 AM Reply

PHP: Main

Level: Beginner
Category: Specific Projects
Notes: '*' are my personal notes, it's suggested to delete them if you're copying this.

When I just finished my previous topic I thought I also needed to make something for the beginners and which might be helpful aswell.
One things I had in mind was something about line breaks. I guess everyone who has a website were people can have a profile message/BBS signature or whatever, there can alwasy be problems with the line breaks. People just might press enter for 1000 times so you're whole layout would be messed up - and that is something we don't want, right? ;)

Let me help you out of this problem, because I found a perfect solution. Again, it's simple, but effective.

What does this script?
This scripts literally counts how many times the enter has been pressed and how many line breaks there are.

Where can I use this script?
Where ever you want. But I suggest using it for text/messages submitted by your visitors/users. A perfect tool to prevent seeing your layout being messed up because of the many enters/line breaks.

Before we start
A small note: a line break in PHP is stated as "\n". So we're not looking for <br \> tags.

We do need a form, so people are able to submit a small text.

- PHP Code -

<?PHP

# They have submitted the form.
if ($_POST['Submit']) {

$max_lines = 5;

$text = $_POST['text'];
$text = strip_tags($text); * Just for protection.
$text .= "\n";

$check = explode("\n", $text);
$lines = count($check);

* Here we count how many lines there are.

# Error process.
if ($lines > $max_lines) {
echo 'Your textnature contained more than '.$max_lines.' line breaks. You used '.$lines.' line breaks.';
exit;
}

# No errors have been found, so process.
$text = str_replace("<br \>", "\n", $text);

* Now here something important happens. HTML doesn't reconize \n so, replace all \n-tags with the HTML's line breaker (<br \>).

$text = trim($text);
$text = nl2br($text);

* Add another <br> at the end.

echo 'Success - You have successfully printed a message!<br><br>
Text: '.$text.'';
exit;

} # End for if ($_POST['Submit']) {

else {

* Nothing has been submitted, so just show the form where the user can submit their text.

echo '<form method="post" action="' . $PHP_SELF . '" name"line_break_check">
<table border="0" cellpadding="2" cellspacing="0">
<tr>
<td align="center" valign="top">Text</td>
</tr>
<tr>
<td><textarea name="text" cols="40" rows="5">Add more than 4 line breaks to see the error.</textarea></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<tr>
</table>';

}
?>

And we're done.

Special Notes
This script has been fully tested and you can find a perfect working example here.

If you're interested in the source of the online example, you can view it here.

If you have comments/problems/complaintments, please let me know.
That was it, I hope I'd helped you a bit!

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Php: Counting Line Breaks Jan. 9th, 2006 @ 08:24 PM Reply

At 1/9/06 05:30 PM, SpamBurger wrote: Cool. Very nice tutorial. But, is there a way that if someone presses enter, it will add a line break?

Isn't that what nl2br() does, or maybe I don't know what you mean.


BBS Signature
EviLDoG
EviLDoG
  • Member since: Oct. 18, 2000
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to Php: Counting Line Breaks Jan. 10th, 2006 @ 06:49 AM Reply

At 1/9/06 08:24 PM, GamesCool wrote:
At 1/9/06 05:30 PM, SpamBurger wrote: Cool. Very nice tutorial. But, is there a way that if someone presses enter, it will add a line break?
Isn't that what nl2br() does, or maybe I don't know what you mean.

That's correct and i think that's what he means.

This also replaces multiple breaks and turns them into 2 line breaks:
$text = str_replace ("\r", NULL, $text); // Get round the windows \r bug
$text = ereg_replace ("\n[\n]+", "\n\n", $text);

Can someone come up with a preg_replace for my ereg please, just to make it faster :), i'm not too good with preg.

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
EviLDoG
EviLDoG
  • Member since: Oct. 18, 2000
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to Php: Counting Line Breaks Jan. 10th, 2006 @ 04:15 PM Reply

At 1/10/06 03:02 PM, Nino_JoJ wrote: How about wordwrap() ?

That would be totally useless! All that does is stop people from putting in loooooooooooooooooooooooooong words and changes it to
shhhhhhh
hooooooo
oooooorr
rrrttttt
words

perj
perj
  • Member since: Dec. 9, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to Php: Counting Line Breaks Jan. 22nd, 2006 @ 08:58 PM Reply

what does exit; do

Afro-Ninja
Afro-Ninja
  • Member since: Mar. 2, 2002
  • Offline.
Forum Stats
Moderator
Level 44
Game Developer
Response to Php: Counting Line Breaks Jan. 22nd, 2006 @ 09:03 PM Reply

At 1/22/06 08:58 PM, perj wrote: what does exit; do

stops the current script from executing any farther


BBS Signature
WoogieNoogie
WoogieNoogie
  • Member since: Jun. 26, 2005
  • Offline.
Forum Stats
Supporter
Level 15
Programmer
Response to Php: Counting Line Breaks Jan. 22nd, 2006 @ 10:33 PM Reply

At 1/22/06 09:03 PM, Afro_Ninja wrote:
At 1/22/06 08:58 PM, perj wrote: what does exit; do
stops the current script from executing any farther

Wouldn't die() do the same thing?

EviLDoG
EviLDoG
  • Member since: Oct. 18, 2000
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to Php: Counting Line Breaks Jan. 23rd, 2006 @ 07:52 AM Reply

At 1/22/06 10:33 PM, WoogieNoogie wrote: Wouldn't die() do the same thing?

Yeah, but die() is an alias for exit(), so exit was around before, but i just guess die() is used in some other languages, thats probably why they did it.

CyberLemming
CyberLemming
  • Member since: Aug. 9, 2005
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Php: Counting Line Breaks Mar. 25th, 2006 @ 06:25 AM Reply

only criticisms:
you havent used stripslashes() or anything on the output.
the go back link is javascript. eh.. why? You may aswell just use a link to PHP_SELF

CyberLemming
CyberLemming
  • Member since: Aug. 9, 2005
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Php: Counting Line Breaks Mar. 25th, 2006 @ 06:27 AM Reply

(The thing about JS is your online example)

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Response to Php: Counting Line Breaks Mar. 25th, 2006 @ 06:48 AM Reply

At 3/25/06 06:25 AM, CyberLemming wrote: only criticisms:
you havent used stripslashes() or anything on the output.

I never added slashes in the first place. I didn't found that necessary.
Why strip slashes if they aren't even there.

the go back link is javascript. eh.. why? You may aswell just use a link to PHP_SELF

What's the difference? It just returns you one page in your browsing history.
Besides, it doesn't affect the code that counts the line breaks, does it? It was only for the example. Feel free to change it; but I don't see any problems with it.

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Counting Line Breaks Mar. 25th, 2006 @ 08:12 AM Reply

At 1/23/06 07:52 AM, EviLDoG wrote:
At 1/22/06 10:33 PM, WoogieNoogie wrote: Wouldn't die() do the same thing?
Yeah, but die() is an alias for exit(), so exit was around before, but i just guess die() is used in some other languages, thats probably why they did it.

Not correct, I think.

PHP is similar to C(++) and in it C it'll be

----
return 0;
----

die() is an alias for exit. However, I always use die();

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Counting Line Breaks Mar. 25th, 2006 @ 04:54 PM Reply

At 3/25/06 04:49 PM, -Calum- wrote: Nice one, I'll use this, but another problem I get a lot is people posting this:

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
zzz (for ages)
Do you know of a way to stop this problem?

Why not?

You fill in your table with the forbidden words, and then use LIKE sql statement to search if there is that word in the forbidden list .
Count rows, and if not dzero, use

substr ("$some var", 0, 10);

Something like that.

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Counting Line Breaks Mar. 25th, 2006 @ 05:01 PM Reply

At 3/25/06 04:58 PM, -Calum- wrote:
At 3/25/06 04:54 PM, Nino_JoJ wrote: stuff...
My news system is courtesy of SpamBurger, and he decided to use flat files (I would too, they're easyer :P), so using SQL to solve this isn't really an option.

EDIT: preg_replace is maybe better

Well, flat-files...
Then you're probably using the implode() and explode() function. I bet you do. But you still may store the forbidden wors in a field.
And do the same thing.

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Counting Line Breaks Mar. 25th, 2006 @ 05:30 PM Reply

At 3/25/06 05:23 PM, -Calum- wrote:
"23098380298903238908902380392809320389209
832089" then it comes up like this "230983802 9890323890 8902380392 80932038 9209832089" (or something similar)

But that's easy!

It'll look like:

<?
$string = "": //ur string
//define stuff
$num = "Every x Characters the word will be changed";
// example
$num = "20": //would make an action every 20 chars in $string word
$type = " "; // an space.

$wrap = wordwrap($string, $num. $type);
echo $wrap;
?>

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Counting Line Breaks Mar. 25th, 2006 @ 05:32 PM Reply

At 3/25/06 05:30 PM, Nino_JoJ wrote: $type = " "; // an space.

Note: " " isn't same as "". While " " is not empty string (it has a whitespace), "" is an empty string.

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to Php: Counting Line Breaks Feb. 28th, 2007 @ 12:48 PM Reply

At 1/10/06 06:49 AM, EviLDoG wrote: This also replaces multiple breaks and turns them into 2 line breaks:
$text = str_replace ("\r", NULL, $text); // Get round the windows \r bug
$text = ereg_replace ("\n[\n]+", "\n\n", $text);

Can someone come up with a preg_replace for my ereg please, just to make it faster :), i'm not too good with preg.

$pattern = '/\n{2,}/';
$replacement = "\n\n";
$text = preg_replace($pattern, $replacement, $text);


BBS Signature