如何从PHP网站获取特定条目?

时间:2022-11-29 22:43:58

can anybody help me to get an specific entry from a website with PHP?

任何人都可以帮助我从PHP网站获取特定条目?

These Information is on the website: "price": "10.03" I want to extract only the price: 10.03.

这些信息在网站上:“价格”:“10.03”我只想提取价格:10.03。

I want to use Regex. I am testing my regular expression with an online tool. But is not right: ("price": ")[0-9][.]*

我想用Regex。我正在使用在线工具测试我的正则表达式。但是不对:(“价格”:“)[0-9] [。] *

Can anybody help me?

有谁能够帮助我?

2 个解决方案

#1


1  

This will work

这会奏效

"price": "(\d+(?:\.\d+)?)

Regex Demo

You can also use

你也可以使用

"price": "([^"]+)

PHP Code

$re = "/\"price\": \"(\\d+(?:\\.\\d+)?)/"; 
$str = "\"price\": \"10.03\""; 
preg_match($re, $str, $matches);
print_r($matches[1]);

Ideone Demo

#2


1  

The content is formatted in json, use json_decode, i.e.:

内容格式为json,使用json_decode,即:

<?php
$json = '{"price": "10.03"}';
$decodeJson = json_decode($json, true);
echo $decodeJson['price'];
//10.03

Ideone Demo

#1


1  

This will work

这会奏效

"price": "(\d+(?:\.\d+)?)

Regex Demo

You can also use

你也可以使用

"price": "([^"]+)

PHP Code

$re = "/\"price\": \"(\\d+(?:\\.\\d+)?)/"; 
$str = "\"price\": \"10.03\""; 
preg_match($re, $str, $matches);
print_r($matches[1]);

Ideone Demo

#2


1  

The content is formatted in json, use json_decode, i.e.:

内容格式为json,使用json_decode,即:

<?php
$json = '{"price": "10.03"}';
$decodeJson = json_decode($json, true);
echo $decodeJson['price'];
//10.03

Ideone Demo