PHP使用前缀标签链接文本


PHP using prefix tags to linkify text

我正在努力为自己的个人使用编写一个代码库,并试图想出一个链接URL和邮件链接的解决方案。我本来打算使用regex语句将URL和邮件地址转换为链接,但担心会覆盖所有基础。所以我现在的想法可能是使用某种标签系统,比如:

l: www.google.com成为http://www.google.com其中m:john.doe@domain.com变成john.doe@domain.com.

你觉得这个解决方案怎么样?你能帮助表达吗?(REGEX不是我的强项)。如有任何帮助,我们将不胜感激。

也许是这样的正则表达式:

$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
    }
}