[英]How to do regex in following Data using PHP
// [ { "id": "715320" ,"t" : "500268" ,"e" : "BOM" ,"l" : "15.55" ,"l_cur" : "Rs.15.55"
,"ltt":"3:59PM IST" ,"lt" : "Sep 9, 3:59PM IST" ,"c" : "+1.69" ,"cp" : "12.19"
,"ccol" : "chg" } ]
I need to Get each with name and assign the value to each 我需要获取每个带有名称的名称并将其分配给每个名称
Like 喜欢
$id=715320; $ ID = 715320;
$e=BOM; $ E = BOM;
from above data, how can i do that? 从以上数据中,我该怎么做?
As that's JSON encoded data, you can use json_decode rather than a regex - this is far more reliable (be sure to remove the leading \\\\
as that's a comment rather than JSON). 由于这是JSON编码的数据,因此您可以使用json_decode而不是正则表达式-这要可靠得多(请确保删除开头的
\\\\
因为这是注释而不是JSON)。
To then get the data into named variables: 然后将数据放入命名变量中:
$array = json_decode($string, true);
foreach ($array as $k => $v){
$$k = $v;
}
This will result in id
, t
etc (which are now keys in $array) being set as their own variables like $id
, $t
. 这将导致
id
, t
等(现在是$ array中的键)被设置为它们自己的变量,例如$id
, $t
。
edit: as aularon notes, you can also use the extract method to move the array keys to the global scope. 编辑:作为aularon注释,您还可以使用extract方法将数组键移到全局范围。
Your string totally looks like a JSon data. 您的字符串看起来完全像一个JSon数据。 You should use the JSon methods to parse the content.
您应该使用JSon方法来解析内容。
If your really want to use regexes you can do this : 如果您真的想使用正则表达式,可以执行以下操作:
<?php
$yourString = '// [ { "id": "715320" ,"t" : "500268" ,"e" : "BOM" ,"l" : "15.55" ,"l_cur" : "Rs.15.55"
,"ltt":"3:59PM IST" ,"lt" : "Sep 9, 3:59PM IST" ,"c" : "+1.69" ,"cp" : "12.19"
,"ccol" : "chg" } ] ';
preg_match_all("/\"(.+?)\"\s*:\s*\"(.+?)\"/", $yourString, $matches, PREG_SET_ORDER);
foreach($matches as $match){
$resultArray[$match[1]] = $match[2];
}
print_r($resultArray);
?>
Is used an array instead of variables in this code, but if you do really want to use variables such as $e
which is a really really bad idea, you can use variable variables . 在此代码中使用数组代替变量,但是如果您确实想使用
$e
变量,这是一个非常糟糕的主意,则可以使用variable variables 。
You replace the foreach content by this : ${$match[1]} = $match[2];
您可以通过以下方式替换foreach内容:
${$match[1]} = $match[2];
Resources : 资源:
On the same topic : 在同一主题上:
Isn't that simply JSON? 这不就是JSON吗? Then take a look at json_decode .
然后看看json_decode 。 Or is there a reason you need to do this with a Regex?
还是有必要使用Regex来执行此操作?
You don't need regular expressions, above is a JSON object notation, you need to do php json_decode
on it, get the values, which will be an associative array: 您不需要正则表达式,上面是一个JSON对象表示法,您需要对其进行php
json_decode
,获取值,这将是一个关联数组:
$str='[ { "id": "715320" ,"t" : "500268" ,"e" : "BOM" ,"l" : "15.55" ,"l_cur" : "Rs.15.55"
,"ltt":"3:59PM IST" ,"lt" : "Sep 9, 3:59PM IST" ,"c" : "+1.69" ,"cp" : "12.19"
,"ccol" : "chg" } ] ';
$data=json_decode($str, true); //true to get the data as associative arrays rather than objects.
$data = $data[0];//since it's an object inside an array
from now on it's recommended keeping this as an associative array, and access objects using array accessors: 从现在开始,建议将其保留为关联数组,并使用数组访问器访问对象:
$id = $data['id'];
PHP's extract
function can be used to extract those values into current context, but that is dangerous, sticking to the accessing the array directly is a better idea. PHP的
extract
函数可用于将这些值提取到当前上下文中,但这很危险,坚持直接访问数组是一个更好的主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.