Convert clickable anchor tags to plain text in html document(将可点击的锚标签转换为 html 文档中的纯文本)
问题描述
我试图在我的内容中匹配 <a>
标签,并将它们替换为链接文本,后跟方括号中的 url 用于打印版本.
I am trying to match <a>
tags within my content and replace them with the link text followed by the url in square brackets for a print-version.
如果只有href",则以下示例有效.如果 <a>
包含另一个属性,则匹配太多并且不会返回所需的结果.
The following example works if there is only the "href". If the <a>
contains another attribute, it matches too much and doesn't return the desired result.
如何匹配 URL 和链接文本,仅此而已?
How can I match the URL and the link text and that's it?
这是我的代码:
<?php
$content = '<a href="http://www.website.com">This is a text link</a>';
$result = preg_replace('/<a href="(http://[A-Za-z0-9\.:/]{1,})">([\s\S]*?)</a>/',
'<strong>\2</strong> [\1]', $content);
echo $result;
?>
想要的结果:
<strong>This is a text link </strong> [http://www.website.com]
推荐答案
您可以使用 ?
使匹配变得不贪婪.您还应该考虑到 href
属性之前可能有一些属性.
You can make the match ungreedy using ?
.
You should also take into account there may be attributes before the href
attribute.
$result = preg_replace('/<a [^>]*?href="(http://[A-Za-z0-9\.:/]+?)">([\s\S]*?)</a>/',
'<strong>\2</strong> [\1]', $content);
这篇关于将可点击的锚标签转换为 html 文档中的纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将可点击的锚标签转换为 html 文档中的纯文本


基础教程推荐
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01