简体   繁体   中英

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.

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

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() :

    $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.

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.

Code: ( Demo )

$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 . Whenever you need to develop a regex pattern, run your tests @ https://regex101.com like this: https://regex101.com/r/31pNLa/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM