<?php $a = array( ‘陆金所‘, ‘有利网‘, ‘安捷财富‘ ); $b = array( ‘<a href="http://www.baidu1.com">陆金所</a>‘, ‘<a href="http://www.baidu2.com">有利网</a>‘, ‘<a href="http://www.baidu3.com">安捷财富</a>‘ ); $str = ‘有投友报料说,央视点名提示股市风险,并指出陆金所、有利网、安捷财富P2P网贷更安全。安捷财富是何方大神可以在安全上与陆金所齐名。‘; echo str_replace_limit($a, $b, $str, 1); // 限制替换次数的函数 function str_replace_limit($search, $replace, $subject, $limit=-1) { if (is_array($search)) { foreach ($search as $k=>$v) { $search[$k] = ‘`‘ . preg_quote($search[$k],‘`‘) . ‘`‘; } } else { $search = ‘`‘ . preg_quote($search,‘`‘) . ‘`‘; } return preg_replace($search, $replace, $subject, $limit); } ?>
时间: 2024-10-10 09:22:46