Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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 preg_match-如果匹配,则返回结果?_Php_Function_Preg Match - Fatal编程技术网

PHP preg_match-如果匹配,则返回结果?

PHP preg_match-如果匹配,则返回结果?,php,function,preg-match,Php,Function,Preg Match,我试图显示屏幕的超链接,但仅当相应的ID记录在字符串中时 <?php function check($i, $s) { if (preg_match('/'.$i.'/',$test)) echo $s; } $test = "000,001,002,003,004,005"; check("001","<a href=''>This is in the test string - 001</a>"); check("003","<a href='

我试图显示屏幕的超链接,但仅当相应的ID记录在字符串中时

<?php
function check($i, $s) {
    if (preg_match('/'.$i.'/',$test)) echo $s;
}

$test = "000,001,002,003,004,005";

check("001","<a href=''>This is in the test string - 001</a>");
check("003","<a href=''>This is in the test string - 003</a>");
check("006","<a href=''>This is in the test string - 006</a>");
check("020","<a href=''>This is in the test string - 020</a>");
?>

所需的输出将是:

<a href=''>This is in the test string - 001</a>
<a href=''>This is in the test string - 003</a>

因为它们是与字符串中的值唯一匹配的两个

这不管用。。你能告诉我为什么以及如何让它工作吗


感谢您没有在函数中定义变量$test。你可以做个比方

<?php
function check($i, $s, $test) {
    if (preg_match('/'.$i.'/',$test)) echo $s;
}

$test = "000,001,002,003,004,005";

check("001","<a href=''>This is in the test string - 001</a>", $test);
check("003","<a href=''>This is in the test string - 003</a>", $test);
check("006","<a href=''>This is in the test string - 006</a>", $test);
check("020","<a href=''>This is in the test string - 020</a>", $test);
?>

为什么您没有为通知启用错误报告?所有这些的目标是什么?谢谢-完全错过了打开错误报告的机会!
Notice: Undefined variable: test in ... on line ...