I have 20 lines of string seperated with "\n"
I have 2 fields to put this string into
$field1 is limited to 10 lines of string
$field2 can store unlimited lines of string
How do I run a php code to seperate the string into 2 fields.
The 1st-10th line will be stored in $field1.
The 11th-20th line will carry over to $field2.
$array = explode("\n", $text);
$field1 = implode("\n", array_slice($array, 0, 10));
$field2 = implode("\n", array_slice($array, 10));
explode
by "\n"
,$field1
$field2
$file_array = file($file_name);
$int = 10;
foreach ($file_array as $line)
{
if($int >=0){
array_push($field1,$line);
$int--;
}else{
array_push($field2,$line);
}
}
You can always trim the lines to get rid of the \n.
<?php
preg_match('/^((?:[^\n]*\n){10})([\w\W]*)$/', $str, $matches);
$field1 = $matches[1];
$field2 = $matches[2];
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.