1
4

4 回答 4

2
$string = preg_replace("/<a(.*?)>/", "<a$1 target=\"_blank\">", $string);

这将添加一个额外的target=\"_blank\"incase 它已经设置。

$string = preg_replace("/<a (href=".*?").*?>/", "<a $1 target="_blank">", $string);

这将确保target="_blank"在 URL 中只添加一个

例如:- http://www.phpliveregex.com/p/6qG

于 2014-08-20T09:21:02.803 回答
1

这将添加目标:

$string = preg_replace("/<a(.*?)>/", "<a$1 target=\"_blank\">", $string);

这是一种检测 URL 并将它们变成链接的粗略方法(这很脆弱):

$string = preg_replace("/(http[^\ ]+)/", "<a href=\"$1\" target=\"_blank\">$1</a>", $string);
于 2012-06-01T06:50:38.740 回答
0

First, I would use some XML/HTML processing library, to get text between tags, then using simple regex:

PHP validation/regex for URL

make all URLs as links.

于 2012-06-01T06:45:14.627 回答
0
$result = preg_replace(
  "/(?<![\>https?:\/\/|href=\"'])(?<http>(https?:[\/][\/]|www\.)([a-z]|[A-Z]|[0-9]|[\/.&?= ]|[~])*)/",
  "<a href=\"$1\">$1</a>",
  $string
);
于 2012-06-01T10:11:09.570 回答