Diego Piffaretti
Diego Piffaretti

Reputation: 15

Parsing HTML page with PHP

I have a simple HTML web page [example.com] that shows some informations and it looks like a CSV file format. each line has a "{" srtarting and a "}" ending. Between the {} there is some information separated by comma "," .

Example(I have a HTML website[example.com] exactly this format):

{"user":"John","country":"EUA","city":"Las Vegas"}
{"user":"Alfred","country":"Italy","city":"Rome"}

The lucky is the HTML body content of the [example.com] is in an easy format to make the parsing.

I would like now a PHP to go to this [example.com], and separete each line by the "{" "}" and after separete each information (user,country and city) and store it in a variables or an array. (In the reallity i will send it to a Database, but it i can do, my problem is to make the parsing of this page).

ps: I have started a php code:

$html = file_get_html('example.com');

But i dont now afeter how to separete each line and each information.

Can someone help me?

Upvotes: 0

Views: 74

Answers (1)

som
som

Reputation: 2437

This looks like JSON, not HTML. Your PHP would be something like:

$json = file_get_contents('example.com');
$data_array = json_decode($json, true);

echo $data_array[0]["user"]; // "John"

see http://php.net/manual/en/function.json-decode.php

** Still getting an error? Check there are commas between each object and a containing array (see below), if not more will need to be done.

[
  {"user":"John","country":"EUA","city":"Las Vegas"},
  {"user":"Alfred","country":"Italy","city":"Rome"}
]

Upvotes: 1

Related Questions