preg_replace 超链接问题
preg_replace issues with hyperlinks
在我决定 post 我在 Whosebug 上的第一个问题之前,我完全疯了,所以这里是:
我有一个网站使用 Google 日历 API 来获取事件条目。在描述部分,用户可以填写有关事件的一些详细信息。现在我希望他们能够放入超链接。这可以通过几种方式发生。
用户类型:
www.test.com
http(s)://test.com
http(s)://www.test.com
<a href="[one of above]" target="_blank">test.com</a>
我正在拼命寻找的是一种将它们全部转换为 <a href="[http:// OR https:// OR www.]test.com" target="_blank">test.com</a>
格式的方法
我已经尝试过这种语法:
$pattern = "/(?i)\b((!<?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
$description = preg_replace($pattern, "<a href='http://'></a>", $description);
它 更改正确超链接中 www.test.com 格式的所有事件,不 更改格式中的事件格式为 http(s)://www.test.com 和 http(s)://test.com。输入格式 <a href="[one of above]" target="_blank">test.com</a>
变得一团糟。
现在我又伤心又累。如果有人能帮助我,你真是个天才!
尝试用方括号括住 s:
$pattern = "/(?i)\b((!<?:http[s]?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
或者:
$description = preg_replace('/(http[s]?:\/\/[^\s]*)/i', '<a href=""></a>', $description);
您可以使用:
$input = preg_replace('/^(?:https?://)?((?:[\p{L}\p{N}-]+\.)+[\p{L}\p{N}-]+)\b/',
'<a href="http://" target="_blank">test.com</a>', $input);
终于用PHP中的条件解决了它:这是代码,所以其他人也可以使用它。现在是否在 .domain/ 之后添加路径。
非常感谢您的帮助!
if (!stristr($description, '<a href')){
$pattern = "~(?:https?://)?((?:[\p{L}\p{N}-]+\.)+[\p{L}-]{2,5})((\S)*)~m";
$description = preg_replace($pattern, "<a href=\"[=10=]\" target=\"_blank\">[=10=]</a>", $description);
}
在我决定 post 我在 Whosebug 上的第一个问题之前,我完全疯了,所以这里是:
我有一个网站使用 Google 日历 API 来获取事件条目。在描述部分,用户可以填写有关事件的一些详细信息。现在我希望他们能够放入超链接。这可以通过几种方式发生。 用户类型:
www.test.com
http(s)://test.com
http(s)://www.test.com
<a href="[one of above]" target="_blank">test.com</a>
我正在拼命寻找的是一种将它们全部转换为 <a href="[http:// OR https:// OR www.]test.com" target="_blank">test.com</a>
我已经尝试过这种语法:
$pattern = "/(?i)\b((!<?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
$description = preg_replace($pattern, "<a href='http://'></a>", $description);
它 更改正确超链接中 www.test.com 格式的所有事件,不 更改格式中的事件格式为 http(s)://www.test.com 和 http(s)://test.com。输入格式 <a href="[one of above]" target="_blank">test.com</a>
变得一团糟。
现在我又伤心又累。如果有人能帮助我,你真是个天才!
尝试用方括号括住 s:
$pattern = "/(?i)\b((!<?:http[s]?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
或者:
$description = preg_replace('/(http[s]?:\/\/[^\s]*)/i', '<a href=""></a>', $description);
您可以使用:
$input = preg_replace('/^(?:https?://)?((?:[\p{L}\p{N}-]+\.)+[\p{L}\p{N}-]+)\b/',
'<a href="http://" target="_blank">test.com</a>', $input);
终于用PHP中的条件解决了它:这是代码,所以其他人也可以使用它。现在是否在 .domain/ 之后添加路径。
非常感谢您的帮助!
if (!stristr($description, '<a href')){
$pattern = "~(?:https?://)?((?:[\p{L}\p{N}-]+\.)+[\p{L}-]{2,5})((\S)*)~m";
$description = preg_replace($pattern, "<a href=\"[=10=]\" target=\"_blank\">[=10=]</a>", $description);
}