我试图在屏幕上显示超链接,但前提是相应的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);
?>
开发时应该使用error_reporting(E_ALL);
,在这种情况下,您会看到以下消息:
Notice: Undefined variable: test in ... on line ...