本文介紹了php html 從文本創建鏈接的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我找到了在文本中找到鏈接時創建 html 鏈接的例程
I found a routine to create a html link when a link is found in a text
<?php
function makelink($text)
{
return preg_replace('/(http://[a-zA-Z0-9_-.]*?) /i', '<a href="$1">$1</a> ', $text." ");
}
// works
echo makelink ("hello how http://www.guruk.com ");
// dont work
echo makelink ("hello how http://www.guruk.com/test.php ");
?>
如您在示例中所見,它僅適用于域,而不適用于該鏈接中存在頁面或子目錄的情況.
as you see in the example, it works find with a domain only, not when there is a page or subdirectory is within that link.
您能否為該功能提供一個解決方案,使其也適用于頁面和子目錄?
Can you provide a solution for that function to work also with pages and subdirs?
謝謝克里斯
推薦答案
字符 ?=&
用于帶有查詢字符串的 url.請注意,我將分隔符從 /
更改為 !
因為您的表達式中有很多斜線.另請注意,如果您處于不區分大小寫的模式,則不需要 A-Z
.
The characters ?=&
are for urls with query strings. Notice that I changed the separator from /
to !
because there a lot of slashes in your expression. Also note that you don't need A-Z
if you are in case-insensitive mode.
return preg_replace('!(http://[a-z0-9_./?=&-]+)!i', '<a href="$1">$1</a> ', $text." ");
這篇關于php html 從文本創建鏈接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!