我试图执行多个搜索和替换字符串给定的前缀列表。preg_match多个搜索替换字符串
例如:
$string = "CHG000000135733, CHG000000135822, CHG000000135823";
if (preg_match('/((CHG|INC|HD|TSK)0+)(\d+)/', $string, $id)) {
# $id[0] - CHG.*
# $id[1] - CHG(0+)
# $id[2] - CHG
# $id[3] - \d+ # excludes zeros
$newline = preg_replace("/($id[3])/","<a href=\"http://www.url.com/newline.php?id=".$id[0]."\">\\1</a>", $string);
}
这仅改变CHG000000135733。我怎样才能使代码工作,以取代其他两个CHG号码作为其相应号码的链接。
使用Casimir et Hippolyte提交的这段代码求解。
$newline = preg_replace ('~(?:CHG|INC|HD|TSK)0++(\d++)~', '<a href="http://www.url.com/newline.php?id=$0">$0</a>', $string);
'$ input'中有什么?你永远不会定义它。 – 2013-04-29 00:01:00