php - php用空白替换所有使用正则表达式的链接
问题描述
以下用于删除所有 HTML 链接的代码$content
不起作用:
$content = str_replace('/<a href=.*?>(.*?)<\/a>/', '', $content);
如果我在单个域或单个字符串上使用它,它就会起作用。
$content = str_replace('http://youtube.com', 'http://example.com', $content);
解决方案
<?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;
?>
推荐阅读
- elm - 防止我的 Elm 应用程序在第 3 方更改 DOM 时崩溃
- ios - 为什么从数组绑定对象时子视图会显示旧数据?
- python - XGBClassifier 中的 'objective' = 'binary:logitraw' 是什么?
- javascript - Html 表格到 Excel 导出脚本显示错误“缺少关闭引号”
- android - 无法访问类“android.widget.fragment”。检查您的模块类路径是否存在缺失或冲突的依赖项
- mysql - 使用 CTE 在时间戳查询上创建索引
- android - 如果我在 onResource 准备好新的 Glide 下载,我应该下载两次还是只下载一次?
- amazon-web-services - 如何在 zsh 中为多个键值导出设置一个导出名称(基于环境)?
- python - python selenium中的StaleElementReferenceException
- c# - 标签转换返回错误“输入字符串的格式不正确。”