我试图向屏幕显示超链接,但前提是相应的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>
因为它们是字符串中值的唯一两个匹配项。
这不起作用..你能告诉为什么以及如何让它发挥作用。?
由于
答案 0 :(得分:2)
您尚未在函数中定义变量$ 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);
?>
开发时应使用error_reporting(E_ALL);
,在这种情况下,您会看到以下消息:
Notice: Undefined variable: test in ... on line ...