Php preg_用超链接替换问题
在我决定将我的第一个问题发布到StackOverflow上之前,我完全疯了,所以这里是: 我有一个网站,使用谷歌日历API获取事件条目。在“说明”部分,用户可以填写有关事件的一些详细信息。现在,我希望他们能够把超链接。这可以通过两种方式实现。 用户类型:Php preg_用超链接替换问题,php,regex,preg-replace,Php,Regex,Preg Replace,在我决定将我的第一个问题发布到StackOverflow上之前,我完全疯了,所以这里是: 我有一个网站,使用谷歌日历API获取事件条目。在“说明”部分,用户可以填写有关事件的一些详细信息。现在,我希望他们能够把超链接。这可以通过两种方式实现。 用户类型: www.test.com http://test.com http://www.test.com 我拼命寻找的是一种将它们全部转换为 我已经尝试过这种语法: $pattern = "/(?i)\b((!<?:https?:\/\/|
www.test.com
http://test.com
http://www.test.com
我已经尝试过这种语法:
$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://$1'>$1</a>", $description);
$pattern=“/(?i)\b((!尝试用方括号括起s:
$pattern = "/(?i)\b((!<?:http[s]?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
$pattern=“/(?i)\b((!您可以使用:
$input = preg_replace('/^(?:https?://)?((?:[\p{L}\p{N}-]+\.)+[\p{L}\p{N}-]+)\b/',
'<a href="http://$1" target="_blank">test.com</a>', $input);
$input=preg\u replace('/^(?:https?:/)?((?:[\p{L}\p{N}-]+\)+[\p{L}\p{N}-]+)\b/',
“$投入);
最终用PHP中的一个条件解决了这个问题:下面是代码,其他人也可以使用它。确实在.domain/now之后添加了路径。
非常感谢你的帮助
if (!stristr($description, '<a href')){
$pattern = "~(?:https?://)?((?:[\\p{L}\\p{N}-]+\\.)+[\\p{L}-]{2,5})((\\S)*)~m";
$description = preg_replace($pattern, "<a href=\"$0\" target=\"_blank\">$0</a>", $description);
}
if(!stristr($description,”,$description);
}
非常感谢您!我的代码现在是(?:非常有用的链接!非常感谢,今晚将播放它!
if (!stristr($description, '<a href')){
$pattern = "~(?:https?://)?((?:[\\p{L}\\p{N}-]+\\.)+[\\p{L}-]{2,5})((\\S)*)~m";
$description = preg_replace($pattern, "<a href=\"$0\" target=\"_blank\">$0</a>", $description);
}