[英]How to use regex or preg_match in PHP to extract exact parts of a string as a variables?
echo(addslashes($dataItem->calculation));
Returns:
"if(([item-237_isChecked])){(2350)}
if(([item-238_isChecked])){(3000)}
if(([item-236_isChecked])&&([item-242_isChecked])){(1090)}
if(([item-236_isChecked])&&([item-243_isChecked])){(2860)}
if(([item-239_isChecked])){(4000)}"
$dataItem->calculation
comes from the database. $dataItem->calculation
来自数据库。
I am trying to extract the values (2350,3000,1090,2860,4000)
as separate variables (those values can change, so I want to match anything for example 我正在尝试将值
(2350,3000,1090,2860,4000)
提取为单独的变量(这些值可以更改,因此我想匹配任何东西)
between 之间
if(([item-237_isChecked])){(
and 和
")}" ).
Did a bunch of testing, but didn't succeed: 做了大量测试,但没有成功:
$calculationString1 = preg_match('/if(([item-237_isChecked])){((.*?))}/', addslashes($dataItem->calculation));
UPDATE: I solved the problem by simplifying my input with explode()
: 更新:我通过使用
explode()
简化了输入来解决了这个问题:
$somestring1 = explode(PHP_EOL, addslashes($dataItem->calculation));
$somesubstring1 = explode('{',$somestring1[0]);
$somesubstring2 = explode('{',$somestring1[1]);
$somesubstring3 = explode('{',$somestring1[2]);
$somesubstring4 = explode('{',$somestring1[3]);
$somesubstring5 = explode('{',$somestring1[4]);
preg_match('/[0-9]{1,6}+/', $somesubstring3[1], $singlelongprice1);
preg_match('/[0-9]{1,6}+/', $somesubstring4[1], $singlewidelongprice1);
preg_match('/[0-9]{1,6}+/', $somesubstring1[1], $doublelongprice1);
preg_match('/[0-9]{1,6}+/', $somesubstring2[1], $triplelongprice1);
preg_match('/[0-9]{1,6}+/', $somesubstring5[1], $quodlongprice1);
$singlelongprice = implode($singlelongprice1);
$singlewidelongprice = implode($singlewidelongprice1);
$doublelongprice = implode($doublelongprice1);
$triplelongprice = implode($triplelongprice1);
$quodlongprice = implode($quodlongprice1);
It looks like isolating your targeted digital strings doesn't require matching the right-side )}
so I am omitting that part from my pattern. 看起来,隔离目标数字字符串不需要匹配右侧
)}
因此我从我的模式中省略了该部分。 I am matching {(
, then restarting the fullstring match with \\K
, then matching the consecutive digits. 我正在匹配
{(
,然后用\\K
重新开始全字符串匹配,然后匹配连续的数字。
After checking if preg_match_all()
makes exactly 5 matches, I assign the 5 fullstring matches ( [0]
is the fullstring matches subarray), and assign them to variables using the modern technique of array deconstructing -- this can be replace with the less-modern list()
call. 在检查
preg_match_all()
是否精确匹配了5个匹配项之后,我分配了5个全字符串匹配项( [0]
是全字符串匹配子数组),并使用现代的数组解构技术将它们分配给变量-可以用较少的替换-现代list()
调用。
$data = 'if(([item-237_isChecked])){(2350)}
if(([item-238_isChecked])){(3000)}
if(([item-236_isChecked])&&([item-242_isChecked])){(1090)}
if(([item-236_isChecked])&&([item-243_isChecked])){(2860)}
if(([item-239_isChecked])){(4000)};';
[$singlelongprice1, $singlewidelongprice1, $doublelongprice1, $triplelongprice1, $quadlongprice1] = preg_match_all('~{\(\K\d+~', $data, $matches) == 5 ? $matches[0] : ['','','','',''];
echo '$singlelongprice1 = ' , $singlelongprice1 , "\n";
echo '$singlewidelongprice1 = ' , $singlewidelongprice1 , "\n";
echo '$doublelongprice1 = ' , $doublelongprice1 , "\n";
echo '$triplelongprice1 = ' , $triplelongprice1 , "\n";
echo '$quadlongprice1 = ' , $quadlongprice1;
Output: 输出:
$singlelongprice1 = 2350
$singlewidelongprice1 = 3000
$doublelongprice1 = 1090
$triplelongprice1 = 2860
$quadlongprice1 = 4000
ps I changed quod
to quad
. PS我改变
quod
到quad
。 Whenever you need to develop a regex pattern, run your tests @ https://regex101.com like this: https://regex101.com/r/31pNLa/1 每当你需要制定一个正则表达式模式,运行测试@ https://regex101.com这样的: https://regex101.com/r/31pNLa/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.