在 Php 正则表达式上提取错误字符串(preg_match_all 函数)

发布于 2024-12-13 18:07:18 字数 1091 浏览 0 评论 0原文

我有一个数组

$arr1[0]=SOAP-ENV:Server Market 1
$arr[1]="";
$arr[2]=0;
$arr[3]="|Error : User Name and Password is Failed"

第二个只是普通变量: K i 合并:

$collarr = array_merged($var1,$othervar);
print_r($collarr);

$collarr 结果 :

Array ( 
    [0] => SOAP-ENV:Server Market 1| 
    [1] =>
    [2] => 0
    [3] => |Error : User Name and Password is Failed
    [4] => SOAP-ENV:Server Market 2| 0 |Error : Failed Mysql query()) 

我用正则表达式解析

preg_match_all("/[error]+/i",$collarr[0].$collarr[4],$resultarr);

BUT i只需得到结果:

 Array ( [0] => Array ( [0] => Error [1] => Error ) )

获得结果如的最佳正则表达式模式是什么:

"Error Soap-Env:Server market 1: User Name and Password is Failed Server Market"."Error Soap-Env:Server market 2 : Failed Mysql query()"

谢谢。

I have an array:

$arr1[0]=SOAP-ENV:Server Market 1
$arr[1]="";
$arr[2]=0;
$arr[3]="|Error : User Name and Password is Failed"

the second just normal variable:
K
i merge:

$collarr = array_merged($var1,$othervar);
print_r($collarr);

$collarr result :

Array ( 
    [0] => SOAP-ENV:Server Market 1| 
    [1] =>
    [2] => 0
    [3] => |Error : User Name and Password is Failed
    [4] => SOAP-ENV:Server Market 2| 0 |Error : Failed Mysql query()) 

i parse with regex

preg_match_all("/[error]+/i",$collarr[0].$collarr[4],$resultarr);

BUT i just get the result :

 Array ( [0] => Array ( [0] => Error [1] => Error ) )

What is the best regex pattern to have for have result like:

"Error Soap-Env:Server market 1: User Name and Password is Failed Server Market"."Error Soap-Env:Server market 2 : Failed Mysql query()"

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2024-12-20 18:07:18

看一下函数 preg_grep,它过滤出指定模式的数组:

$resultarr = preg_grep("/error/i", $collarr);
print_r($resultarr);

为我打印:

Array
(
    [3] => |Error : User Name and Password is Failed
    [4] => SOAP-ENV:Server Market 2| 0 |Error : Failed Mysql query()
)

Look at the funciont preg_grep, it filters out the array for the specified pattern:

$resultarr = preg_grep("/error/i", $collarr);
print_r($resultarr);

Prints for me:

Array
(
    [3] => |Error : User Name and Password is Failed
    [4] => SOAP-ENV:Server Market 2| 0 |Error : Failed Mysql query()
)
往日情怀 2024-12-20 18:07:18

可以使用包含 preg_match_all()preg_grep() 的方法,但这些函数使用正则表达式,通常比非正则表达式效率低 -为基础的函数。

如果您始终具有与示例数组中提供的相同的数据结构,那么我有一个使用 array_filter(), implode()< /a> 和 str_replace() 为每个错误数组提供所需的字符串。

$arr1[0]="SOAP-ENV:Server Market 1";
$arr1[1]="";
$arr1[2]=0;
$arr1[3]="|Error : User Name and Password is Failed";

echo $str1=str_replace("|","",implode(' ',array_filter($arr1)));  // declare and echo

echo "<br>";

$arr2[0]="SOAP-ENV:Server Market 2";
$arr2[1]="";
$arr2[2]=0;
$arr2[3]="|Error : Failed Mysql query()";

echo $str2=str_replace("|","",implode(' ',array_filter($arr2)));  // declare and echo

这将输出:

SOAP-ENV:Server Market 1 Error : User Name and Password is Failed
SOAP-ENV:Server Market 2 Error : Failed Mysql query()

您会看到,如果将 4 元素数组提供给我的 3 函数方法,它将首先删除空/空/零值,然后使用空格作为粘合将剩余元素组合到将被剥离的字符串中它的所有管道字符。

如果您打算对这些数据进行任何串联/组合/合并,我建议您在生成所需的字符串后执行此操作。

如果您的实际案例具有更多样化的值,并且我的答案无法适应您项目的复杂性,请使用一些导致麻烦的示例输入更新您的问题,我将调整我的方法。

You could use methods that include preg_match_all() and preg_grep(), but those function use regex and are generally less efficient than non-regex-based functions.

If you always have the same data structure that you provided in your sample arrays, then I have a one-liner solution that uses array_filter(), implode(), and str_replace() to provide your desired string for each error array.

$arr1[0]="SOAP-ENV:Server Market 1";
$arr1[1]="";
$arr1[2]=0;
$arr1[3]="|Error : User Name and Password is Failed";

echo $str1=str_replace("|","",implode(' ',array_filter($arr1)));  // declare and echo

echo "<br>";

$arr2[0]="SOAP-ENV:Server Market 2";
$arr2[1]="";
$arr2[2]=0;
$arr2[3]="|Error : Failed Mysql query()";

echo $str2=str_replace("|","",implode(' ',array_filter($arr2)));  // declare and echo

This will output:

SOAP-ENV:Server Market 1 Error : User Name and Password is Failed
SOAP-ENV:Server Market 2 Error : Failed Mysql query()

You see that if you feed the 4-element array to my 3-function method, it will first remove empty/null/zero values, then combine the remaining elements using a space as glue into a string that will be stripped of all of its pipe characters.

If you are going to do any concatenation/combination/merging of this data, I recommend doing so after the desired string has been generated.

If your actual case has more diverse values and my answer doesn't accommodate your project's complexity, please update your question with some sample input that causes trouble and I'll adjust my method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文