PHP常用正则表达式
header("Content-Type:text/html;charset=utf-8"),这一句一般都是用于设置页面的字符集,防止出现乱码,虽然跟本节没多大关系,但也可以当作基础知识。
/pic/strong>
1
2
3
$preg = '/^(https?:/pic/i';
$str = 'www.liqingbo.cn';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/^([a-z]+):/pic/i';
$str = '/pic/p>
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';
$str = '255.255.255.250';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
4
$preg = '/^<([a-z]+)([^<]+)*(?:>(.*)</1>|s+/>)$/';
$str = '<a href="/pic/a>';
$res = preg_match_all($preg, $str, $matches);
var_dump($matches);
/pic/strong>
1
2
3
4
5
$preg = '/<img[^>]+(src="([^"<>']+)"|src='([^"<>']+)')[^<>]*>/';
$html = '<div><a href="/pic/pic/src/img0.gif" /><img src="/pic/src/img1.gif" /></a></div>';
$res = preg_match_all($preg, $html, $matches, PREG_PATTERN_ORDER);
/pic/p>
echo $matches[2][0]; /pic/p>
/pic/strong>
1
2
3
$preg = '/^([a-z0-9_.-]+)@([a-z0-9.-]+).([a-z]+)$/i';
$str = 'jeddy_liu-jin@gmail.com';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/^[a-z0-9@_.-]{6,18}$/';
$str = 'liujin@1234.com';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/^[a-z0-9_-]{3,16}$/';
$str = 'liujin-88';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/^(0d{2,3})-?(d{7,8})$/';
$str = '015-5415488';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/^1[3|4|5|8]d{9}$/';
$str = '18012345678';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/^[1-9]d{5}$/';
$str = '415000';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/(^d{15}$)|(^d{18}$)/';
$str = '430701198806520';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
4
$preg = '/^[x{4e00}-x{9fa5}]+$/u';
$str = 'PHP博客';
preg_match($preg, $str, $match);
var_dump($match);
【PHP常用正则表达式】相关文章:
PHP常用的正则表达式11-06
PHP知识:PHP常用正则表达式大全03-16
PHP常用的正则表达式是什么02-26
用php常用表单验证的正则表达式02-27
php汉字正则表达式12-08
php正则表达式的基本语法总结12-02
PHP知识:正则表达式基础知识12-05
php过滤HTML标签、属性等正则表达式02-22
在PHP中使用正则表达式进行查找替换03-15