
正文
php提取文本中某个数据 php提取文本中某个数据的值
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
请教php大神,如何提取一个json数据里的某值?
需要准备的材料分别是:电脑、php编辑器、浏览器。
1、首先,打开php编辑器,新建php文件,例如:index.php,填充问题基础代码。
2、在index.php中,输入代码:
$b = json_decode($a);
echo $b-content-location-lat;
echo ',';
echo $b-content-location-lng;
3、浏览器运行index.php页面,此时lng和lat的值都被打印了出来。
相关问答
Q1: php如何读取文本指定的内容?
php读取文件内容:
-----第一种方法-----fread()--------
?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));//指定读取大小,这里把整个文件内容读取出来
echo $str = str_replace("\r\n","br /",$str);
}
?
--------第二种方法------------
?php
$file_path = "test.txt";
if(file_exists($file_path)){
$str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
$str = str_replace("\r\n","br /",$str);
echo $str;
}
?
-----第三种方法------------
?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = "";
$buffer = 1024;//每次读取 1024 字节
while(!feof($fp)){//循环读取,直至读取完整个文件
$str .= fread($fp,$buffer);
}
$str = str_replace("\r\n","br /",$str);
echo $str;
}
?
-------第四种方法--------------
?php
$file_path = "test.txt";
if(file_exists($file_path)){
$file_arr = file($file_path);
for($i=0;$icount($file_arr);$i++){//逐行读取文件内容
echo $file_arr[$i]."br /";
}
/*
foreach($file_arr as $value){
echo $value."br /";
}*/
}
?
----第五种方法--------------------
?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str ="";
while(!feof($fp)){
$str .= fgets($fp);//逐行读取。如果fgets不写length参数,默认是读取1k。
}
$str = str_replace("\r\n","br /",$str);
echo $str;
}
?
Q2: php读取文本指定的值
你用下面php提取文本中某个数据的代码试试php提取文本中某个数据:
$json= '{"createForm":{"multiple":1,"shareType":"SELF","title":"","description":"","minSubscriptionCost":"0.00","subscriptionCost":"0.00","commissionRate":"0.00","baodiCost":"0.00","secretType":"FULL_PUBLIC","mode":"COMPOUND","content":"1:02,11\r\n1:01,10\r\n1:01,03\r\n1:08,02\r\n1:01,03\r\n1:05,06\r\n1:08,09\r\n1:02,01\r\n1:07,06\r\n1:07,04","periodId":48308,"units":10,"schemeCost":20,"playType":"RandomTwo"},"lotteryType_key":"sdel11to5","ajax":"true"}';
$data = json_decode($json, true);
$content = $data['createForm']['content'];
// 注意,这里的$content中包含"\r\n",如果你是保存到普通文件,则php提取文本中某个数据他本身就是换行符,所以直接将$content保存到文件即可,如果你是在页面中输出,则有两种方式
// 方式1
$contents = explode('\r\n');
foreach ($contents as $v) {
echo $v,'br /';
}
// 方式2
$content = preg_replace('/\r\n/', 'br /', $content);
echo $content;
php提取文本中某个数据的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php提取文本中某个数据的值、php提取文本中某个数据的信息别忘了在本站进行查找喔。








