php data filtering and Count

Asked

Viewed 40 times

0

Good is the following I have a web app where graphics Gero, however I liked to filter data and do coutagens, I have only data in a comic, so I have already exported and to avoid q app slow I was thinking of reading from csv but I need to filter the data someone knows how I can do ex:

csv

Nome, profissão, Idade,Sexo ,Nacionalidade , quantidade
jose , trolha,10,M,nacional,10
jose , trolha,10,M,nacional,10
jose , trolhaassda1,10,M,nacional,10
jose , trolha1dsds,10,M,nacional,10
jose , trolh1111,10,M,nacional,10
jose , trolhadasdas,10,M,nacional,10

wanted that appears only the trolley. it is possible

output Name, profession, Age, Gender ,Nationality , quantity Jose , troll,10,M,national,10 Jose , troll,10,M,national,10

1 answer

0

The most performative way to do this would be by using a database and filtering the data you need.

In the case of using a file it is possible to use a regex to extract the data you need or read the entire file and use the PHP strpos function.

Ex. in Mysql:

SELECT * FROM tabela_csv WHERE profissao LIKE '%trolha%';

Ex. in PHP using Regex:

$regex = "(.*?)\ ,\ (.*?)trolha(.*?),(.*?),(.*?),(.*?),(.*?)\n";
preg_match_all($regex, $csvContent, $matches);
  • and to maintain the header?

  • Yes, it is possible to keep the header since you will just format the information output

Browser other questions tagged

You are not signed in. Login or sign up in order to post.