sbi*_*nko 4 html php regex url
我找到了一个自动检测链接并将它们放在<a>标签中的解决方案:Regex PHP - 自动检测YouTube,图像和"常规"链接
相关部分(preg_replace_callback出于兼容性原因,我不得不在调用之外移动函数):
function put_url_in_a($arr)
{
if(strpos($arr[0], 'http://') !== 0)
{
$arr[0] = 'http://' . $arr[0];
}
$url = parse_url($arr[0]);
//links
return sprintf('<a href="%1$s">%1$s</a>', $arr[0]);
}
$s = preg_replace_callback('#(?:https?://\S+)|(?:www.\S+)|(?:\S+\.\S+)#', 'put_url_in_a', $s);
Run Code Online (Sandbox Code Playgroud)
这样可以正常工作,除非它偶然发现标签中的网址,然后将其标记为废弃(通过在其中添加另一个标记).它也破坏了嵌入式媒体.
问题:如何使用此函数排除HTML标记,希望只使用正则表达式?
一个选项 - 如果URL已经在链接中,则必须以前缀为前缀href=',因此请排除具有负向lookbehind断言的链接:
#(?<!href\=['"])(?:https?://\S+)|(?:www.\S+)|(?:\S+\.\S+)#
Run Code Online (Sandbox Code Playgroud)
编辑: - 实际上上面的表格不起作用,因为URL匹配太笼统,它会把事情...变成一个链接,不正确.使用我自己喜欢的URL匹配方案似乎正常工作:
$s = preg_replace_callback('#(?<!href\=[\'"])(https?|ftp|file)://[-A-Za-z0-9+&@\#/%()?=~_|$!:,.;]*[-A-Za-z0-9+&@\#/%()=~_|$]#', 'regexp_url_search', $s);
Run Code Online (Sandbox Code Playgroud)
例如:http://codepad.viper-7.com/TukPdY
$s = "The following link should be linkified: http://www.google.com but not this one: <a href='http://www.google.com'>google</a>."`
Run Code Online (Sandbox Code Playgroud)
变为:
The following link should be linkified: <a href="http://www.google.com">http://www.google.com</a> but not this one: <a href='http://www.google.com'>google</a>.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5769 次 |
| 最近记录: |