首页 > 解决方案 > php用空白替换所有使用正则表达式的链接

问题描述

以下用于删除所有 HTML 链接的代码$content不起作用:

$content = str_replace('/<a href=.*?>(.*?)<\/a>/', '', $content);

如果我在单个域或单个字符串上使用它,它就会起作用。

$content = str_replace('http://youtube.com', 'http://example.com', $content);

标签: php

解决方案


使用preg_replace

<?php
$content = "this is a sample <a href=\"\">link</A> end";
$result=preg_replace('/(<a .*href=["\'])([^> ]*)([\'"]>)([^<]*)(<\/a>)/i', '\1http://www.example.com\3\4\5', $content); 
echo $result;
?>

推荐阅读