-3

Possible Duplicate:
How to put string in array, split by new line?

Is it possible when using the $_REQUEST[''] to only request one row from a text area?

Something like:

<?php 

  $text = $_REQUEST['originalText']; 

?>

<textarea name="firstRow" rows="5" cols="50" wrap="OFF" ><?php 
    echo $text
?></textarea>

<form action="index.php" method="POST" name="editText">
  <input name="submit" value="Edit Text" type="submit"><br>
  <textarea name="originalText" rows="5" cols="50" wrap="OFF"></textarea>
</form>

Instead of echo $text is it possible to echo just the first row of $text?

Community
  • 1
  • 1
Koala
  • 5,253
  • 4
  • 25
  • 34
  • What have you tried? Where are you stuck at? Have you searched here on the site? – PeeHaa Dec 23 '12 at 14:13
  • I don't know where to start, Ive got it so I get the whole textarea to the new textarea (code above) but I don't know how to just move the first row over, instead of the whole thing. – Koala Dec 23 '12 at 14:14
  • 1
    http://php.net/string and http://php.net/strings – hakre Dec 23 '12 at 14:15

2 Answers2

3

I don't understand exactly what you mean by first row, but if you want to get the first line (before an "enter") do the following:

list($first_line) = explode("\n", $text, 2);

Or alternatively (and preferably):

$first_line = strstr($text, "\n", true);

See strtrDocs and Demo - you might want to add a "\n" to the $text to ensure at least one line exists.

hakre
  • 193,403
  • 52
  • 435
  • 836
DanielX2010
  • 1,897
  • 1
  • 24
  • 26
0

This code works too.

<?php
 if(isset($_REQUEST['text'])){
    $text = $_REQUEST['text'];
    $line1 = strpos($text, "\n");
    if($line1 !== false){
        echo substr($text, 0, $line1);
    } else {
        echo $text;
    }
    exit;
 }
?>
<form>
    <textarea name="text"></textarea>
    <input type="submit" />
</form>
Hamidreza
  • 1,825
  • 4
  • 29
  • 40