在一段文本中,我们有几个链接,可以找到:
$regex = '\b(http://www.domain.com/)[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]';
我们想将这些 url 更改为新的,例如:
http://www.domain.com/news/item.php?ID=12321&TYPE=25进入:/news/page/$arttype-$artID/
$url 列出了其中几个 url,但我们似乎无法在 $message 中更新它们。
这是到目前为止的代码:
$string = "$message";
function do_reg($text, $regex) {
preg_match_all($regex, $text, $result, PREG_PATTERN_ORDER);
return $result[0];
}
$A =do_reg($string, $regex);
foreach($A as $url) {
$check = parse_url($url, PHP_URL_QUERY);
preg_match("/ID=([^&]+)/i", $check, $matches);
$artID = $matches[1];
preg_match("/TYPE=([^&]+)/i", $check, $matches);
$arttype = $matches[1];
preg_replace("$url", "/news/page/$arttype-$artID/", $text);
}
有谁知道如何更新在 $message 中找到的所有唯一 url?
--------使用V-tech的代码----
$message = "
<li><strong><a href="http://www.domain.com/news/item.php?ID=12321&TYPE=25" target="_blank">Link 1</a></li>
<li><strong></strong><a href="http://www.domain.com/news/item.php?ID=12300&TYPE=2" target="_blank">Link 2</a></li>
<li><a href="http://www.domain.com/news/item.php?ID=12304&TYPE=2" target="_blank">Link 3</a></li>
<li><a href="http://www.domain.com/news/item.php?ID=12314&TYPE=2" target="_blank">Link 4</a></li>";
$pattern = "/(http:\/\/www\.domain\.com)\/news\/item\.php\?ID=([^&]+)&TYPE=(\d+)/g";
$replacement = "\${1}/news/page/\${2}-\${3}/";
preg_replace($pattern, $replacement, $message);
echo "$message ";