P
P
Petro Boyko2017-12-22 18:43:59
PHP
Petro Boyko, 2017-12-22 18:43:59

What is the correct way to parse a csv file?

How to parse csv file? The file is on another site, this is such a centralization with settings, I
try to parse like this

$myurl = $sym_link . "data/soc_links.csv";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myurl);
curl_setopt($curl, CURLOPT_REFERER, $sym_link);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
// разбиваем по строкам
$res = explode("\r\n",$response);
foreach($res as $line)
{
  $elem = explode("<|>",$line);
  //var_dump($elem);
  echo ("<li><a href=".$elem[1]." target='_blank'>".$elem[2]."</a></li>");
}

displays RSS 2
var_dump well, so it is in the file
1<|>#<|>RSS 
2<|>#<|>Facebook 
3<|>#<|>Twitter 
4<|>#<|>Google+ 
5<|>#<|>Flickr

array(11) { [0]=> string(1) "1" [1]=> string(1) "#" [2]=> string(6) "RSS 2" [3]=> string(1 ) "#" [4]=> string(11) "Facebook 3" [5]=> string(1) "#" [6]=> string(10) "Twitter 4" [7]=> string(1 ) "#" [8]=> string(10) "Google+ 5" [9]=> string(1) "#" [10]=> string(7) "Flickr " }
and if so
$cat = file("data/soc_links.csv");

foreach($cat as $line)
{
  $elem = explode("<|>",$line);
  echo ("<li><a href=".$elem[1]." target='_blank'>".$elem[2]."</a></li>");
}

outputs normally RSS Facebook Twitter Google+ Flickr
what would it be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2017-12-22
@Sanasol

php.net/manual/en/function.fgetcsv.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question