利用 preg_match(),我们可以完成字符串的规则匹配。如果找到一个匹配,preg_match() 函数返回 1,否则返回 0。还有一个可选的第三参数可以让你把匹配的部分存在一个数组中。在验证数据时这个功能可以变得非常有用。
实例代码如下:
- <?php
- // 模式定界符后面的 "i" 表示不区分大小写字母的搜索
- if (preg_match ("/php/i", "PHP is the web scripting language of choice.")) {
- print "A match was found.";
- } else {
- print "A match was not found.";
- }
- ?>
取得当前时间
实例代码如下:
- <?php
- //需要匹配的字符串。date函数返回当前时间。 "现在时刻:2012-04-20 07:31 am"
- $content = "现在时刻:".date("Y-m-d h:i a");
- //匹配日期和时间.
- if (preg_match ("/d{4}-d{2}-d{2} d{2}:d{2} [ap]m/", $content, $m))
- {
- echo "匹配的时间是:" .$m[0]. "n"; //"2012-04-20 07:31 am"
- }
- //分别取得日期和时间
- if (preg_match ("/([d-]{10}) ([d:]{5} [ap]m)/", $content, $m))
- {
- echo "当前日期是:" .$m[1]. "n"; //"2012-04-20"
- echo "当前时间是:" .$m[2]. "n"; //"07:31 am"
- }
- ?>
这个例子将验证出此 Email 地址为正确格式。现在让我们来看看这段正则表达式所代表的各种规则。
获取Google首页title
比如说要获取google首页的title内容,代码如下:
实例代码如下:
- <?php
- $str = file_get_contents('http://www.google.com');
- preg_match('/<title>(.*)</title>/', $str, $arr);
- echo $arr[1];
- ?>
从网址获取域名
实例代码如下:
- <?php
- preg_match("/^(http://)?([^/]+)/i", "http://www.111cn.net/index.html", $matches);
- $host = $matches[2]; // 从主机名中取得后面两段
- preg_match("/[^./]+.[^./]+$/", $host, $matches);
- echo "domain name is: {$matches[0]}n";
- ?>
preg_match($pattern,$string,$matcher)其中$pattern对应的就是/^(http://)?([^/]+)/i,$string 是http://www.php.net/index.html,$match是匹配到的结果。
如果提供了 matches,则其会被搜索的结果所填充。$matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包
含与第一个捕获的括号中的子模式所匹配的文本,以此类推。
$matches[0] 将包含与整个模式匹配的文本。咱们用pring_r打印出来第一个$matches:
实例代码如下:
- Array (
- [0] => http://www.111cn.net
- [1] => http://
- [2] => http://www.phpfensi.com )
$matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本。在正则中,()代表模式:匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。就是说数组中下标为1的值就是正则中/^(http://)?([^/]+)/i第一个()里的值!数组下标2的值以此类推。
波比源码 » php正则表达式之preg_match()用法
levofloxacin oral brand levaquin 250mg
avodart 0.5mg tablet ondansetron price zofran 8mg pills
aldactone 100mg brand oral finasteride 1mg purchase fluconazole pill
order ampicillin pill buy sulfamethoxazole generic cost erythromycin 250mg
order sildenafil 100mg online cheap bimatoprost ca methocarbamol 500mg for sale
sildenafil tablet estradiol 1mg generic order estradiol online
lamotrigine 50mg pill mebendazole drug retin over the counter
purchase tadalis generic generic avanafil buy diclofenac without prescription
indomethacin order online lamisil usa purchase amoxicillin online cheap
tadalafil 20mg oral Buy viagra in canada viagra for women
tadalafil 40mg pour homme acheter 25mg gГ©nГ©rique viagra en france generique sildenafil 200mg
cheap doxycycline 200mg buy clomid 100mg generic furosemide price
purchase ramipril online irbesartan oral astelin 10ml drug
buy generic buspirone 5mg amiodarone 100mg cost brand ditropan
alendronate 35mg uk order fosamax 70mg online cheap pepcid online
order molnunat generic buy molnupiravir without prescription buy prevacid 15mg online cheap
tadalafil 20mg oral buy prozac 20mg sildenafil dosage
cialis 40mg price order tadalafil for sale order symmetrel 100 mg generic
naltrexone 50 mg usa purchase abilify pill abilify 20mg canada
cialis for sales order generic betnovate buy anafranil 25mg sale
cost chloroquine 250mg olumiant 2mg ca baricitinib price
glycomet 1000mg oral female cialis tadalafil order cialis 10mg generic
norvasc online order tadalafil 20mg usa cialis 10mg drug
clozaril 100mg brand order ipratropium 100 mcg pill decadron 0,5 mg price
order omeprazole 20mg pills pay for essays online blackjack with real money
zyvox drug online casino gambling online casino roulette
clomid 100mg pills purchase albuterol without prescription best online poker sites
tadalafil pill usa viagra overnight viagra 50mg sale
order aristocort sale desloratadine online buy clarinex tablet
dapoxetine 90mg brand priligy price order synthroid 100mcg sale
cheap orlistat acyclovir 400mg brand zovirax for sale
order tadalafil 20mg generic order plavix 150mg pill purchase plavix without prescription
buy domperidone 10mg generic sumycin 250mg generic purchase flexeril generic
gloperba us play for real online casino games free casino games
buy ranitidine 300mg generic purchase zantac pill oral celecoxib 200mg
that roulette online blackjack game play casino games for cash
brand tamsulosin 0.2mg oral zofran spironolactone 100mg canada
metronidazole cost metronidazole online buy oral bactrim 480mg
diflucan over the counter sildenafil 100mg brand viagra online order
purchase cephalexin online cheap erythromycin 250mg uk erythromycin 250mg canada
tadalafil 5 mg tablet cialis 20mg uk viagra in usa
sildenafil medication buy viagra 100mg generic cialis online order