熟習Regex的大俠看過來

來源:互聯網
上載者:User
熟悉Regex的大俠看過來
小弟遇到點問題

現在從資料庫讀取一段字串,使用Regex判斷是否含有“http://”字串
並且處理成標籤可點擊的

目前小弟糾結的是,如果連續的“http://”挨在一起正則就只能判斷為一個串連了,不知道該怎麼處理了-――

水平有限啊。。。

例如“http://dfhakoadghttp://daidgjiadpugidua”

識別成http://dfhakoadg和http://daidgjiadpugidua

分享到:


------解決方案--------------------

//只要保證你的url含有http開頭的話,可以不使用正則:
$s = 'http://dfhakoadghttp://daidgjiadpugidua';
$a = explode('http://', $s);
foreach($a as $r) {
if($r) echo "http://$r", '
';
}
//http://dfhakoadg
//http://daidgjiadpugidua

------解決方案--------------------
$str="ahahttp://www.chinindfaasdl.comahahttp://www.chinindfaasdl.com
hahahahttp://www.chinindfaasdl.com嗲話地哦862189375829http://www.chinindfaasdl.comhiaohdgioas
862189375829http://www.chinindfaasdl.comhiaohdgioas
撒旦法經理卡資料的分類http://www.chinindfaasdwfdfsdl.com";
$str = preg_replace('/(http:\/\/.*?\.com)/', 'click', $str);
var_dump($str);



不知道你要換哪裡? 我加了個click
------解決方案--------------------
引用:
Quote: 引用:

$str="ahahttp://www.chinindfaasdl.comahahttp://www.chinindfaasdl.com
hahahahttp://www.chinindfaasdl.com嗲話地哦862189375829http://www.chinindfaasdl.comhiaohdgioas
862189375829http://www.chinindfaasdl.comhiaohdgioas
撒旦法經理卡資料的分類http://www.chinindfaasdwfdfsdl.com";
$str = preg_replace('/(http:\/\/.*?\.com)/', 'click', $str);
var_dump($str);



不知道你要換哪裡? 我加了個click
感謝回答,不過只能篩選出.com的話不是我想達到的結果。。。
感覺http後面那一堆要篩選出除了“http://”以外的東西

目前應該不能完美的解決你這個問題。
因為http與下一個之間可能還有其它string,而這些又符合url規則,所以程式根本不能判斷這string是屬於上一個的url,還是兩個url之間的間隔
------解決方案--------------------
$str="ahahttp://www.chinindfaasdl.comahahttp://www.chinindfaasdl.com
hahahahttp://www.chinindfaasdl.com嗲話地哦862189375829http://www.chinindfaasdl.comhiaohdgioas
862189375829http://www.chinindfaasdl.comhiaohdgioas
撒旦法經理卡資料的分類http://www.chinindfaasdwfdfsdl.com";
echo preg_replace('#http://[\w\.-]+\.(com
------解決方案--------------------
net
------解決方案--------------------
org
------解決方案--------------------
hk
------解決方案--------------------
info
------解決方案--------------------
cc
------解決方案--------------------
edu)#s', '$0', $str);
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.