我正在尝试编写一个供我个人使用的代码库,并且我正在尝试提出一个链接 URL 和邮件链接的解决方案。我最初打算使用正则表达式将 URL 和邮件地址转换为链接,但担心覆盖所有基础。所以我目前的想法可能是使用这样的某种标签系统:
l:www.google.com 变成http://www.google.com并且 m:john.doe@domain.com 变成 john.doe@domain.com。
您如何看待这个解决方案,您能协助表达吗?(正则表达式不是我的强项)。任何帮助,将不胜感激。
我正在尝试编写一个供我个人使用的代码库,并且我正在尝试提出一个链接 URL 和邮件链接的解决方案。我最初打算使用正则表达式将 URL 和邮件地址转换为链接,但担心覆盖所有基础。所以我目前的想法可能是使用这样的某种标签系统:
l:www.google.com 变成http://www.google.com并且 m:john.doe@domain.com 变成 john.doe@domain.com。
您如何看待这个解决方案,您能协助表达吗?(正则表达式不是我的强项)。任何帮助,将不胜感激。
也许像这样的一些正则表达式:
$content = "l:www.google.com some text m:john.doe@domain.com some text";
$pattern = '/([a-z])\:([^\s]+)/'; // One caracter followed by ':' and everything who goes next to the ':' which is not a space or tab
if (preg_match_all($pattern, $content, $results))
{
foreach ($results[0] as $key => $result)
{
// $result is the whole matched expression like 'l:www.google.com'
$letter = $results[1][$key];
$content = $results[2][$key];
echo $letter . ' ' . $content . '<br/>';
// You can put str_replace here
}
}