Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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从字符串中提取ID_Php_Regex - Fatal编程技术网

PHP从字符串中提取ID

PHP从字符串中提取ID,php,regex,Php,Regex,例如:Lorem ipsum dolor sit amet[ID:123]Suspendisse[blahsh 68]调味品 我怎么才能拿到123 我在第一部分检查角色位置: $pos = strrpos($l, "ID: "); if ($pos === false) { $id = 123; } 这是最容易用正则表达式处理的 $matches = array(); $pattern = '/^.*\[ID:\s+(\d+)\].*$/'; preg_match($pattern,

例如:Lorem ipsum dolor sit amet[ID:123]Suspendisse[blahsh 68]调味品

我怎么才能拿到123

我在第一部分检查角色位置:

$pos = strrpos($l, "ID: ");
if ($pos === false) {
    $id = 123;
}

这是最容易用正则表达式处理的

$matches = array();
$pattern = '/^.*\[ID:\s+(\d+)\].*$/';
preg_match($pattern, "Lorem ipsum dolor sit amet, [ID: 123] Suspendisse [blahsh 68] condimentu", $matches);


// Your number is in $matches[1]
Array
(
    [0] => Lorem ipsum dolor sit amet, [ID: 123] Suspendisse [blahsh 68] condimentu
    [1] => 123
)
模式通过
\s+
匹配
[ID:
,后跟任意数量的空格。然后
(\d+
捕获数字序列。
[]
括号需要作为
\[\]
转义,因为它们是正则表达式中的元字符

$matches = array();
$pattern = '/^.*\[ID:\s+(\d+)\].*$/';
preg_match($pattern, "Lorem ipsum dolor sit amet, [ID: 123] Suspendisse [blahsh 68] condimentu", $matches);


// Your number is in $matches[1]
Array
(
    [0] => Lorem ipsum dolor sit amet, [ID: 123] Suspendisse [blahsh 68] condimentu
    [1] => 123
)
如果您对表达式的其余部分感兴趣:

  • ^.*
    是字符串的开头,以及后面的任何内容
  • *$
    是在上面已经解释过的匹配部分之后的字符串末尾的任何其他内容

    • 这是最容易用正则表达式处理的

      $matches = array();
      $pattern = '/^.*\[ID:\s+(\d+)\].*$/';
      preg_match($pattern, "Lorem ipsum dolor sit amet, [ID: 123] Suspendisse [blahsh 68] condimentu", $matches);
      
      
      // Your number is in $matches[1]
      Array
      (
          [0] => Lorem ipsum dolor sit amet, [ID: 123] Suspendisse [blahsh 68] condimentu
          [1] => 123
      )
      
      模式通过
      \s+
      匹配
      [ID:
      ,后跟任意数量的空格。然后
      (\d+
      捕获数字序列。
      []
      括号需要作为
      \[\]
      转义,因为它们是正则表达式中的元字符

      $matches = array();
      $pattern = '/^.*\[ID:\s+(\d+)\].*$/';
      preg_match($pattern, "Lorem ipsum dolor sit amet, [ID: 123] Suspendisse [blahsh 68] condimentu", $matches);
      
      
      // Your number is in $matches[1]
      Array
      (
          [0] => Lorem ipsum dolor sit amet, [ID: 123] Suspendisse [blahsh 68] condimentu
          [1] => 123
      )
      
      如果您对表达式的其余部分感兴趣:

      • ^.*
        是字符串的开头,以及后面的任何内容
      • *$
        是在上面已经解释过的匹配部分之后的字符串末尾的任何其他内容

        • 如果字符串始终为[ID:1234],则可能是这样

          $str='Lorem ipsum dolor sit amet, [ID: 123] Suspendisse [blahsh 68] condimentu';
          preg_match_all("~\[ID:\s(.*?\d+)\]~",$str,$match);
          echo $match[1][0];
          

          如果字符串始终为[ID:1234],则可能是这样

          $str='Lorem ipsum dolor sit amet, [ID: 123] Suspendisse [blahsh 68] condimentu';
          preg_match_all("~\[ID:\s(.*?\d+)\]~",$str,$match);
          echo $match[1][0];
          

          我猜我需要学习正则表达式。有人能推荐一个好的开始/引用位置吗?ID总是用方括号括起来吗?@JohnMagnolia是最好的开始位置,尽管它很密集。这是一个非常全面的引用。是的,我试着去学它,但似乎代码中没有逻辑,完全没有gobbledigook。看起来我在猜测我需要学习正则表达式。有人能推荐一个好的开始/引用位置吗?ID总是用方括号括起来吗?@JohnMagnolia是最好的开始位置,尽管它很密集。这是一个非常全面的引用。是的,我尝试过学习它,但它似乎没有逻辑代码和完整的gobbledigook。看起来像