Hi all,
Im learning PHP & SQL very quickly, but still have issues with counting this, and incrementing that.... so if someone could help, you'd be(proverbially) teaching a man(woman in my case..lol) to fish....

Im constructing a form which in part, allows user to input instructions.
I would like the users input to increment with numbering, each time user hits "enter" in the textarea to make a paragraph, so that the output looks something like:

step 1. blah blip blah
<user hits "enter", makes paragraph>
step 2. blah blur blah
<user hits "enter", makes paragraph>

The best example I have is on the website recipezaar.com...

so for example:

which is just an awesome site btw for any of u who like to cook...

If you look to how their "Directions" on recipe pages increment, you will see what I am trying to explain, god help my explainatory abilities. The way in which i explained i would like input incremented, is how they increment it on this website.... user presses "enter"..input is incremented..

If anyone could give me an idea on how I could implement this....very thankful, i would be...

Cheers! Clarity

9 Years
Discussion Span
Last Post by somedude3488

Pretty simple. Check this example.

if(isset($_POST['submit'])) {
	$string = nl2br($_POST['textarea']);
	echo $string."<br />";
	echo substr_count($string, "<br />");
<form method='POST'>
<textarea rows="30" cols="30" name='textarea'>
<input type="submit" name="submit" value="submit">

I am using nl2br function to convert newline to <br />. Then I am counting how many <br />'s are present in the sting. This would give the count of the "Enter" pressed by the user.


on input of the data, you need to replace the newlines "\r\n" with a replaceable string. i have had problems with newlines being forgotten when they are put into the database.

$text = preg_replace( "/(\r\n)/","|NL|",$_POST['text'] ); //where $_POST['text'] is the name of the textarea

then on output, explode the data.

$text = explode( '|NL|',$row['text'] ); //$row['text'] is from the db
$i = 1;
foreach( $text as $txt ) {
  echo "{$i} - {$txt}<br />";

like that? how i would do it


OK, wonderful... thanks for both answers:)
But i dont think i have explained this correctly...
When the input is entered via the textarea, each step a user types is done like this:

Recipe Directions:
Take your eggs, and whisk
<user hits "enter" and makes new line>
Add eggs to flour mix
<user hits "enter" and makes new line>
Beat until lumps are gone.

So i think kkeith29, your answer would solve that, replacing the newlines "\r\n" with a replaceable string and using foreach.


Then I need the data to output with numbering. So as per my example, i want it to output as:

Recipe Directions:

1. Take your eggs & milk, and whisk
2. Add eggs & milk mix to flour mix
3. Beat until lumps are gone.

Each time user creates new line, i want it to increment, & number each line. Hope that makes more sense.


then on output, explode the data.

Oops, I didnt see that before I replied... I guess that answers my problem..

Thank you....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.