简体   繁体   English

preg_replace()在数组上不起作用

[英]preg_replace() not working on array

I have the following code: 我有以下代码:

$pattern_dig_answer = array(); // Identify patterns to replace and their replacements
$pattern_dig_answer[0] = "/(.*)IN(\s+)A(.*)/";
$replacement_dig_answer = array();
$replacement_dig_answer[0] = "<span style='color:red;'>$1IN$2A$3</span>";

The first entry for the pattern_dig_answer array is some regex for the pattern I'm looking to match. pattern_dig_answer数组的第一个条目是我要匹配的模式的一些正则表达式。 The first entry for the replacement_dig_answer is what I am looking to replace what the regex finds with. replace_dig_answer的第一个条目是我要替换的正则表达式的内容。

I then execute a dig like so: 然后执行如下所示的挖掘:

$dig_answer = exec("dig {$_SESSION['domain']} $digrecords[2] $digops[0] $digops[1] $digops[2] $digops[3]", $dig_answer_raw);

Which, as you may guess, executes a dig command with session variables captured from user input and then saves the output to $dig_answer_raw. 您可能会猜到,该命令将使用从用户输入中捕获的会话变量执行dig命令,然后将输出保存到$ dig_answer_raw。

What I now do is clean up the dig with the following: 我现在要做的是用以下方法清理挖坑:

$dig_answer_out = preg_grep('/IN/', $dig_answer_raw);

And then I echo the results: 然后我回显结果:

echo implode("\n", $dig_answer_out);

This all works fine up until this point. 到现在为止,所有这些工作正常。 It's only when I go to do the following that I get stuck: 只有在执行以下操作时,我才会被卡住:

echo preg_replace($pattern_dig_answer, $replacement_dig_answer, $dig_answer_out);

Now, from the investigating I've done, this should work. 现在,根据我所做的调查,这应该可以进行。 The documentation for preg_replace clearly says that it can search either in a normal string or via an array with string entries (which I'm assuming I have). preg_replace的文档明确指出,它可以在普通字符串中搜索,也可以通过带有字符串条目的数组搜索(我假设我已经拥有了)。 What makes me think that I might not is as soon as I change the dig command to this: 是什么让我认为我可能不应该将dig命令更改为:

$dig_answer = shell_exec("dig {$_SESSION['domain']} $digrecords[2] $digops[0] $digops[1] $digops[2] $digops[3]");

It works like a charm, however negates all the output cleaning that preg_grep did previously even after changing around all the variables. 它的工作原理就像一个咒语,但是即使更改了所有变量后,preg_grep之前执行的所有输出清理操作也将无效。

Is there something painfully obvious that I'm missing here? 我在这里想念的地方很痛苦吗?

EDIT: To try and simplify my question, when my dig command is changed from exec(), which saves the output to an array, to shell_exec, which saves the output to a string, then preg_replace works. 编辑:为简化我的问题,当我的dig命令从exec()更改为将输出保存到数组的情况下,更改为shell_exec并将存储输出保存为字符串时,则preg_replace起作用了。 It doesn't seem to want to work in an array and I'm not sure why... 它似乎不想在数组中工作,我不确定为什么...

I reconstructed the script based on your original post: 我根据您的原始帖子重建了脚本:

<?php

$dig_answer = exec("dig www.google.com.au", $dig_answer_raw);

$dig_answer_out = preg_grep('/IN/', $dig_answer_raw);

$pattern_dig_answer = array(); // Identify patterns to replace and their replacements
$pattern_dig_answer[0] = "/(.*)IN(\s+)A(.*)/";
$replacement_dig_answer = array();
$replacement_dig_answer[0] = "<span style='color:red;'>$1IN$2A$3</span>";

$x = preg_replace($pattern_dig_answer, $replacement_dig_answer, $dig_answer_out);
print_r($x);

The output from this script looks like it is working for me: 该脚本的输出似乎对我有用:

Array
(
   [8] => <span style='color:red;'>;www.google.com.au.      IN  A</span>
   [11] => <span style='color:red;'>www.google.com.au.  125 IN  A   203.219.219.163</span>
   ...
)

Am I missing something? 我想念什么吗? For reference, this is the output of php -v for me. 供参考,这是php -v的输出。

PHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15) PHP 5.4.24(CLI)(建立:2014年1月19日21:32:15)

I just noticed that this line may not produce what you are expecting: 我只是注意到这一行可能无法达到您的期望:

echo preg_replace($pattern_dig_answer, $replacement_dig_answer, $dig_answer_out);

In this instance, preg_replace() will return an array as you have passed an array as the first and second arguments. 在这种情况下, preg_replace()将返回一个数组,因为您已将数组作为第一个和第二个参数传递给了它。 Try this instead: 尝试以下方法:

$foo = preg_replace($pattern_dig_answer, $replacement_dig_answer, $dig_answer_out); var_dump($foo);

You've split your code so it's tricky to tell what you're running. 您已经分割了代码,因此很难分辨正在运行的代码。 I think this is it, reconstructed from all of the parts as follows: 我认为是这样,从所有部分重构如下:

<?php
$pattern_dig_answer = array(); // Identify patterns to replace and their replacements
$pattern_dig_answer[0] = "/(.*)IN(\s+)A(.*)/";
$replacement_dig_answer = array();
$replacement_dig_answer[0] = "<span style='color:red;'>$1IN$2A$3</span>";

$dig_answer = exec("dig google.com", $dig_answer_raw);
$dig_answer_out = preg_grep('/IN/', $dig_answer_raw);
echo implode("\n", $dig_answer_out);
echo preg_replace($pattern_dig_answer, $replacement_dig_answer, $dig_answer_out);

When I run this over a CLI (PHP 5.5.11) I get a warning: PHP Notice: Array to string conversion on the line where you're calling echo preg_replace(...); 当我在CLI(PHP 5.5.11)上运行此命令时,收到警告: PHP Notice: Array to string conversion在您调用echo preg_replace(...);的行上, PHP Notice: Array to string conversion echo preg_replace(...); .

You're (implicitly) converting the array that is returned by preg_replace() to a string with echo . 您(隐式)将preg_replace()返回的数组转换为带有echo的字符串。 Since you're passing in an array() for preg_replace for both parameters, you're getting back an array (as you assumed). 由于您要为两个参数的preg_replace传递array() ,因此您将获得一个array (如您假设的那样)。 The best way to dump those to view what is inside is with var_dump() . 转储这些文件以查看内部内容的最佳方法是使用var_dump() If you want to print it in a different way you could use a foreach loop and print that element. 如果要以其他方式打印它,则可以使用foreach循环并打印该元素。

<?php
$pattern_dig_answer = array(); // Identify patterns to replace and their replacements
$pattern_dig_answer[0] = "/(.*)IN(\s+)A(.*)/";
$replacement_dig_answer = array();
$replacement_dig_answer[0] = "<span style='color:red;'>$1IN$2A$3</span>";

$dig_answer = exec("dig google.com", $dig_answer_raw);
$dig_answer_out = preg_grep('/IN/', $dig_answer_raw);
implode("\n", $dig_answer_out);
$total_output = preg_replace($pattern_dig_answer, $replacement_dig_answer, $dig_answer_out);
foreach ($total_output as $output) {
    echo $output."<br />\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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