iT邦幫忙

0

有關把文字的連結轉換成anchor tag包住

  • 分享至 

  • xImage

大家好, 想請教一下如果可以在php先從一堆文字找到文字的連結再轉換成anchor tag包住.
有或者是沒有在link 前加http/https/www 的regex , 謝謝.

看更多先前的討論...收起先前的討論...
ccutmis iT邦高手 2 級 ‧ 2021-07-02 12:30:47 檢舉
狗哥 'php regex replace example'
diu7me iT邦新手 4 級 ‧ 2021-07-02 12:45:09 檢舉
真的找了好久, 找不到
diu7me iT邦新手 4 級 ‧ 2021-07-02 12:49:38 檢舉
是找到一個, 但如果沒有http/https/www這個就找不到
(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?
diu7me iT邦新手 4 級 ‧ 2021-07-02 12:49:54 檢舉
俾如說google.com
froce iT邦大師 1 級 ‧ 2021-07-02 14:34:23 檢舉
"^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$"

搜尋字: domain regexp
https://stackoverflow.com/questions/10306690/what-is-a-regular-expression-which-will-match-a-valid-domain-name-without-a-subd/26987741#26987741

昨天剛好找過。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
japhenchen
iT邦超人 1 級 ‧ 2021-07-02 16:35:19

剛剛好有在玩這遊戲........javascript版,沒動用任何js框架
https://jsfiddle.net/mps6g0uh/15/

HTML

<p>打字打字打字http://www.abc.com.tw/vinvin/x/s/a/c/d/這是網址</p>...但不能直接點

<hr>

<div>

</div>
這就可以點了

javascript

let reg1 = /((https*\:\/\/)+(.+\.*){2,}\/([\w]\/*)+)/ig;
let source = document.getElementsByTagName("p");
let target = document.getElementsByTagName("div");

target[0].innerHTML = source[0].innerText.replace(reg1,'<a href="$1" target="_blank">$1</a>');
看更多先前的回應...收起先前的回應...

範例裡是我瞎掰的網址,居然打的開
/images/emoticon/emoticon04.gif

diu7me iT邦新手 4 級 ‧ 2021-07-02 17:06:42 檢舉

感謝japhen, 謝謝你的火速救援

diu7me iT邦新手 4 級 ‧ 2021-07-02 18:42:35 檢舉

我找了一個
/((https?|ftp)://)?([a-z0-9+!(),;?&=.-]+(:[a-z0-9+!(),;?&=.-]+)?@)?([a-z0-9-.]).(([a-z]{2,4})|([0-9]{1,3}.([0-9]{1,3}).([0-9]{1,3})))(:[0-9]{2,5})?(/([a-z0-9+%-].?)+)/?(?[a-z+&$.-][a-z0-9;:@&%=+/.-]*)?(#[a-z.-][a-z0-9+$%_.-]*)?([\x{4e00}-\x{9fa5}a-zA-Z0-9]+)?/i
但不知道為何我在網上tester測試成功, 但用preg_match過不到.....

網址前後跟普通文字共串的地方是不是有隔空白?

我要發表回答

立即登入回答