php使用正規表示式獲取字串中的URL

2022-10-06 08:39:10 字數 1246 閱讀 2029

執行結果為:

( [0] => array

( [0] => ) )

這裡主要使用到preg_match_all函式,該函式具體使用方法如下:

preg_match_all — 進行全域性正規表示式匹配

語法:int preg_match_all ( string pattern, string subject, array matches [, int flags] )

在 subject程式設計客棧 中搜尋所有與 pattern 給出的正規表示式匹配的內容並將結果以 flags 指定的順序放到 matches 中。

搜尋到第乙個匹配項之後,接下來的搜尋從上乙個匹配項末尾開始。

特別注意preg_pattern_order 和preg_set_order

flags 可以是下列標記的組合(注意把 preg_pattern_order 和 preg_set_order 合起來用沒有意義):

如果使用preg_pattern_order

對結果排序使 $matches[0] 為全部模式匹配的陣列,$matches[1] 為第乙個括號中的子模式所匹配的字串組成的陣列,以此類推。(即$matches[0] [0]為全部模式匹配中的每一項,$matches[0] [1]為全部模式匹配中的第二項,$matches[1] [0]為匹配每乙個括號中的第一項,$matches[1] [0]為匹配每乙個括號中的第二項)

輸出結果:

example: , this is a test

example: , this is a tewww.cppcns.comst

本文標題: php使用正規表示式獲取字串中的url

本文位址: /wangluo/php/175247.html

用正規表示式擷取字串

region 匹配字元 獲取資訊 匹配字元 獲取資訊 被匹配字串 匹配內容 匹配到的資訊 public static string matchfunction string nowhtml text,string matchname catch return values endregion 使用例...

正規表示式擷取字串操作

string str shdfj3h434343hjhjhgfg 第一步,去除空格trim str.trim 定義乙個空的字串 string str1 null if str null equals str system.out.println str1 前面會生成乙個null這個方法只能吧數字提取...

如何用正規表示式擷取字串

有這麼一段字串 數字 字串 結果取 a 數字 b 字串 擷取方法1 int a convert.toint32 txt1.text.trim replace split 1 string b txt1.text.trim replace split 2 擷取方法2 string str 數字 字串 ...