Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
匹配键值对的php正则表达式_Php_Regex - Fatal编程技术网

匹配键值对的php正则表达式

匹配键值对的php正则表达式,php,regex,Php,Regex,我有一根如下的线 $string = "height=175cm weight=70kgs age=25yrs" 字符串内容是键值对,每对由一个制表符分隔。我希望每个键值对作为单独的变量,并打印出每个键值对。 我已经尝试了以下代码,但我没有得到正确的结果,请帮助我哪里出了问题 $string = "height=175cm weight=70kgs age=25yrs"; $pattern = "(([^=]*)\s*=\s*(.*))"; if (preg_match($pattern,$s

我有一根如下的线

$string = "height=175cm weight=70kgs age=25yrs"
字符串内容是键值对,每对由一个制表符分隔。我希望每个键值对作为单独的变量,并打印出每个键值对。 我已经尝试了以下代码,但我没有得到正确的结果,请帮助我哪里出了问题

$string = "height=175cm weight=70kgs age=25yrs";
$pattern = "(([^=]*)\s*=\s*(.*))";
if (preg_match($pattern,$string,$match)) {
    echo "<pre>";
    print_r($match);
} else {
    echo "not matche\n";
}
您可以使用以下代码:

Array
(
    [height] => 175cm
    [weight] => 70kgs
    [age] => 25yrs
)
输出: 您可以使用以下代码:

Array
(
    [height] => 175cm
    [weight] => 70kgs
    [age] => 25yrs
)
输出: 您可以使用以下选项:

array(4) {
  [0]=>
  array(3) {
    [0]=>
    string(12) "height=175cm"
    [1]=>
    string(12) "weight=70kgs"
    [2]=>
    string(9) "age=25yrs"
  }
  [1]=>
  array(3) {
    [0]=>
    string(6) "height"
    [1]=>
    string(6) "weight"
    [2]=>
    string(3) "age"
  }
  [2]=>
  array(3) {
    [0]=>
    string(3) "175"
    [1]=>
    string(2) "70"
    [2]=>
    string(2) "25"
  }
  [3]=>
  array(3) {
    [0]=>
    string(2) "cm"
    [1]=>
    string(3) "kgs"
    [2]=>
    string(3) "yrs"
  }
}
结果:

$string = "height=175cm weight=70kgs    age=25yrs";
if (preg_match_all('/\s*([^=]+)=(\S+)\s*/', $string, $matches)) {
   $output = array_combine ( $matches[1], $matches[2] );
   print_r($output);   
}
<?php

$string = "height=175cm\tweight=70kgs\tage=25yrs";

// Divide your string into an array, with each element
// in the array being a string with a key-value pair
$pairs = explode("\t", $string);

// See what the array of pair strings looks like.
// print_r($pairs); 

// Create an array to get it ready to hold key-value pairs.
$results = array();

// For each string in your array, split at the equal sign
// and set values in the $results array.
foreach ($pairs as $pair) {
  $exploded_pair = explode("=", $pair);

  // See what each exploded pair array looks like.
  // print_r($exploded_pair);

  $key = $exploded_pair[0];
  $value = $exploded_pair[1];
  $results[$key] = $value;
}

print_r($results);
您可以使用以下选项:

array(4) {
  [0]=>
  array(3) {
    [0]=>
    string(12) "height=175cm"
    [1]=>
    string(12) "weight=70kgs"
    [2]=>
    string(9) "age=25yrs"
  }
  [1]=>
  array(3) {
    [0]=>
    string(6) "height"
    [1]=>
    string(6) "weight"
    [2]=>
    string(3) "age"
  }
  [2]=>
  array(3) {
    [0]=>
    string(3) "175"
    [1]=>
    string(2) "70"
    [2]=>
    string(2) "25"
  }
  [3]=>
  array(3) {
    [0]=>
    string(2) "cm"
    [1]=>
    string(3) "kgs"
    [2]=>
    string(3) "yrs"
  }
}
结果:

$string = "height=175cm weight=70kgs    age=25yrs";
if (preg_match_all('/\s*([^=]+)=(\S+)\s*/', $string, $matches)) {
   $output = array_combine ( $matches[1], $matches[2] );
   print_r($output);   
}
<?php

$string = "height=175cm\tweight=70kgs\tage=25yrs";

// Divide your string into an array, with each element
// in the array being a string with a key-value pair
$pairs = explode("\t", $string);

// See what the array of pair strings looks like.
// print_r($pairs); 

// Create an array to get it ready to hold key-value pairs.
$results = array();

// For each string in your array, split at the equal sign
// and set values in the $results array.
foreach ($pairs as $pair) {
  $exploded_pair = explode("=", $pair);

  // See what each exploded pair array looks like.
  // print_r($exploded_pair);

  $key = $exploded_pair[0];
  $value = $exploded_pair[1];
  $results[$key] = $value;
}

print_r($results);

我在下面粘贴了一个代码示例,它可以帮助您解决问题。当然,它的压缩不是很紧,并且比其他答案(都是好答案!)有更多的代码行

我这样做的原因是,看起来你可能会从一个解释中受益,这个解释会让你在解决问题的过程中一步一个脚印地前进,这样你就可以理解过程中发生了什么

以下是您可以使用的代码:

$pairs = explode("\t", $string);


我在下面粘贴了一个代码示例,它可以帮助您解决问题。当然,它的压缩不是很紧,并且比其他答案(都是好答案!)有更多的代码行

我这样做的原因是,看起来你可能会从一个解释中受益,这个解释会让你在解决问题的过程中一步一个脚印地前进,这样你就可以理解过程中发生了什么

以下是您可以使用的代码:

$pairs = explode("\t", $string);


谢谢@M42:是的,我当然可以。现在编辑。谢谢@M42:是的,我当然可以。现在编辑它。