arrays - php how to get whole line after spicific character from text file? -
i have text file
name,name1 willhaveishere1 name,name2 willhaveishere2 name,name3 willhaveishere3
i want read , return that
$nn = name1 $ss = willhaveishere1
with code name1
my code is
$file1 = "file.txt"; $file = file($file1); $count = count($file); if($count > 0) { $i = 1; foreach($file $row) { $n = strstr($row, 'name,'); $cc = array("name,"); $dd = array(""); $nn = str_replace($cc, $dd, $n); echo $nn; $i++; } }
this need
if($count > 0) { foreach($file $row) { $pos = strpos($row, ','); if($pos !== false){ echo substr($row, $pos + 1); $nn[] = substr($row, $pos + 1); } else { echo $row; $ss[] = $row; } } }
edit
yes, loop through, make sure both $nn
, $ss
has same count, depending on file.
also note: mysql_*
functions has been deprecated, please use mysqli
or pdo
instead
$count = count($nn); for($i=0; $i < $count; $i++){ $sql = "insert users(name, line) values('$nn[$i]', '$ss[$i]')"; mysql_query($sql); }
edit 2
try example:
$file = array( 0 => 'name,name1', 1 => 'willhaveishere1', 2 => 'name,name2', 3 => 'willhaveishere2', 4 => 'name,name3', 5 => 'willhaveishere3' ); $count = count($file); if($count > 0) { foreach($file $row) { $pos = strpos($row, ','); if($pos !== false){ $nn[] = substr($row, $pos + 1); } else { $ss[] = $row; } } } echo '<pre>'; $count = count($nn); for($i=0; $i < $count; $i++){ $sql = "insert users(name, line) values('$nn[$i]', '$ss[$i]');"; echo $sql.php_eol; }
Comments
Post a Comment