0

我需要使用正则表达式从另一个字符串中提取一个字符串。这是我的正则表达式:

/^httpamp$/

我查看了 php 函数 preg_filter ( mixed $pattern , mixed $replacement , mixed $subject),但我不明白它是如何工作的。我知道模式是这样的:/^httpamp$/,并且$subject是我的初始字符串,但是替换的是什么?

4

1 回答 1

0

元素指的$replacement是您想要替换模式的内容 - 作为数组或字符串,但您不需要preg_filter()用于标识子字符串。

如果您要识别字符串的一部分(无论长度如何),请使用preg_match()

<?php
  // The string to seach
  $yourString = "the lazy fox";

  // The pattern to match
  $pattern = '/^the/';

  // The function to use
  preg_match($pattern, $yourString, $matches);

  // Output the $matches produced (formatted)
  echo '<pre>';
  print_r($matches);
  echo '</pre>';
?>

您可以使用一些其他选项(标志/偏移),您可以在此处找到更多信息

于 2013-04-12T01:58:54.250 回答