php - 如何使用 RegexIterator::REPLACE 模式?

标签 php spl

我的代码有什么问题:

$i = new RegexIterator(
  new ArrayIterator(array(
    'test1'=>'test888', 
    'test2'=>'what?', 
    'test3'=>'test999')),
  '/^test(.*)/',
  RegexIterator::REPLACE);

foreach ($i as $name=>$value)
  echo $name . '=>' . $value . "\n";

迭代器是空的,为什么?感谢您的帮助!

最佳答案

如果您省略操作模式(新 RegexIterator 语句中的第 3 个参数),您将获得匹配值,如下所示:

$array = array('test1' => 'test888', 'test2' => 'what?', 'test3' => 'test999');
$pattern = '/^test(.*)/';

echo '<pre>';
echo "DEFAULT\n";
$arrayIterator = new ArrayIterator($array);
$regexIterator = new RegexIterator($arrayIterator, $pattern);
foreach ($regexIterator as $value) {echo "$value\n";}
echo '</pre>';

您可以根据需要使用不同的操作模式。继续阅读 setMode 文档:http://www.php.net/manual/en/regexiterator.setmode.php

关于php - 如何使用 RegexIterator::REPLACE 模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1957069/

相关文章:

php - 将多个文件插入/上传到 PHP MYSQL 数据库表中的特定行

php - 如何使用 file_get_contents 而不丢失回显值?

php - cURL 返回空字符串

php - ArrayAccess 如何工作?

php - 哪个 PHP 接口(interface)允许使用数组表示法访问对象的属性?

php - 使用 php 迭代器接口(interface)遍历数据库结果

php - 使用php在jquery自动更正中显示Mysql数据库中存储的url中的图像

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

php - 从 SplFileInfo 对象获取流资源

php - iterator_to_array 截断输出