簡體   English   中英

PHP 使用前綴標簽鏈接文本

[英]PHP using prefix tags to linkify text

我正在嘗試編寫一個供我個人使用的代碼庫,並且我正在嘗試提出一個鏈接化 URL 和郵件鏈接的解決方案。 我原本打算使用正則表達式語句轉到 go 以將 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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM