189 8069 5689

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的值都被打印了出来。

php如何获取txt文本指定行的指定数据?

如果直接使用file_get_contents来读取文件,那么在文件很大的时候会很占内容,比如这个文件有1GB的时候。

这个时候使用传统的文件操作方式就好的多,因为是查找嘛,逐行读取匹配应该也是可以的,下面是我的一个建议,不知道是否满足你的要求,可以看下:

//

需要查找的内容

$search

=

'bcd';

//

打开文件

$res

=

fopen('a.txt',

'r');

while

($line

=

fgets($res,

1024))

{

//

根据规则查找

if

(strpos($line,

$search)

===

0)

{

//

根据既定规则取得需要的数据

echo

substr($line,

4,

-1);

//

这里就是你想得到的

break;

}

}

//

关闭文件

fclose($res);

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",如果你是保存到普通文件,则他本身就是换行符,所以直接将$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读取文件内容:

-----第一种方法-----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;

}

?


新闻名称:php提取文本中某个数据 php提取文本中某个数据的内容
链接地址:http://cdxtjz.cn/article/ddejcci.html

其他资讯