如何在html textarea中逐句获取字符串

时间:2012-08-21 21:04:56

标签: php javascript html string textarea

我的文字区域包含以下信息,

Secured places in the national squad respectively
Selected to the national team this year as well
Selected to the national team twice during his university career
Went to school at ABS 

当用户点击提交按钮时,我希望在提交页面中将每行中的字符串添加到php变量,类似这样

$a = "Secured places in the national squad respectively";
$b = "Selected to the national team this year as well";
$c = "Selected to the national team twice during his university career";
$d = "Went to school at ABS";

任何人都可以告诉我该怎么做?

4 个答案:

答案 0 :(得分:2)

如果你不关心句子而不是换行符,你应该可以基于这样的方式分成一个数组:

$posted_text = $_POST['text']; // or however you get the value into a string
$text_array = explode(PHP_EOL, $posted_text);

答案 1 :(得分:1)

我认为你在寻找

explode('\n\r', $var)

其中var是你撕裂的价值

答案 2 :(得分:0)

你最好的选择是通过换行符来爆炸字符串\ n。不幸的是,如果用户实际上没有按Enter键并且文本刚刚滚动到下一行,则不会有任何换行符被爆炸。

答案 3 :(得分:0)

不破坏线条的工作示例:


$words = explode(" ", str_replace("\n"," ",$text));
$sentences = Array();

$sentence = Array();
foreach($words as $word) {    
    $word = trim($word);
    if(!$word) continue ;
    $char1 = substr($word,0,1);
    $char2 = substr($word,1,1);

    if((strtoupper($char1) == $char1) && (strtolower($char2) == $char2)) {              
        if(count($sentence)) {
            $sentences[] = join(" ", $sentence);
        }        
        $sentence = Array();
    }    
    $sentence[] = $word;    
}
if(count($sentence)) {
    $sentences[] = join(" ", $sentence);
}