PHP preg_match-如果匹配,则返回结果?
我试图显示屏幕的超链接,但仅当相应的ID记录在字符串中时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='
<?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 ...