papaface Posted January 4, 2008 Share Posted January 4, 2008 Hello, How can I read a textareas contents line by line? e.g A link on each line that needs to be parsed separately. Is this the right way to do it? <?php if ($_POST['submit']) { $text = explode("<br />",nl2br($_POST['links'])); foreach ($text as $key => $value) { echo $key . " => " . $value."<br />"; } } else {?> <form name="links" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <textarea name="links" rows="20" cols="50"></textarea> <br /> <input type="submit" name="submit" value="Send"> </form> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84388-textarea-reading-line-by-line-simple-i-know/ Share on other sites More sharing options...
awpti Posted January 4, 2008 Share Posted January 4, 2008 A submitted textarea doesn't contain HTML Linebreaks (br) unless it's actually put in there by the user. \n is what you should be looking for. Quote Link to comment https://forums.phpfreaks.com/topic/84388-textarea-reading-line-by-line-simple-i-know/#findComment-429834 Share on other sites More sharing options...
papaface Posted January 4, 2008 Author Share Posted January 4, 2008 Did you actually read my code? nl2br() converts \n to <br /> ..... Quote Link to comment https://forums.phpfreaks.com/topic/84388-textarea-reading-line-by-line-simple-i-know/#findComment-429837 Share on other sites More sharing options...
duclet Posted January 4, 2008 Share Posted January 4, 2008 That is why he used the function nl2br. Anyway, what you have should work. However, why don't you just to the explode using "\n", it will work as well, just make sure it is around double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/84388-textarea-reading-line-by-line-simple-i-know/#findComment-429838 Share on other sites More sharing options...
awpti Posted January 4, 2008 Share Posted January 4, 2008 *facepalm* Didn't even see the nl2br. Quote Link to comment https://forums.phpfreaks.com/topic/84388-textarea-reading-line-by-line-simple-i-know/#findComment-429840 Share on other sites More sharing options...
papaface Posted January 4, 2008 Author Share Posted January 4, 2008 <?php if ($_POST['submit']) { $text = explode("\n",$_POST['links']); foreach ($text as $key => $value) { echo $key . " => " . $value."<br />"; } } else {?> <form name="links" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <textarea name="links" rows="20" cols="50"></textarea> <br /> <input type="submit" name="submit" value="Send"> </form> <?php } ?> So thats the way to do it? Quote Link to comment https://forums.phpfreaks.com/topic/84388-textarea-reading-line-by-line-simple-i-know/#findComment-429842 Share on other sites More sharing options...
duclet Posted January 4, 2008 Share Posted January 4, 2008 Yes. It produced this output for me when I tested it: 0 => http://www.google.com 1 => http://www.yahoo.com 2 => http://www.phpfreaks.com Quote Link to comment https://forums.phpfreaks.com/topic/84388-textarea-reading-line-by-line-simple-i-know/#findComment-429844 Share on other sites More sharing options...
papaface Posted January 4, 2008 Author Share Posted January 4, 2008 Well I know it works, I am asking if it is the actual best way to do what I want. It seems a bit clumsy to me lol. Quote Link to comment https://forums.phpfreaks.com/topic/84388-textarea-reading-line-by-line-simple-i-know/#findComment-429850 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.