相关疑难解决方法(0)

将内容标记为内容中的纯文本

我正在尝试匹配<a>我的内容中的标签,然后用链接文本替换,然后用方括号中的url替换打印版本.如果只有"href",则以下示例有效.如果<a>包含另一个属性,则它匹配太多并且不会返回所需的结果.如何匹配URL和链接文本呢?

这是我的代码:

<?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;
?> 
Run Code Online (Sandbox Code Playgroud)

期望的结果:

<strong>This is a text link </strong> [http://www.website.com]
Run Code Online (Sandbox Code Playgroud)

谢谢你,杰森

php regex preg-replace

0
推荐指数
1
解决办法
3971
查看次数

标签 统计

php ×1

preg-replace ×1

regex ×1