1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| $str = 'abc[url]123[/url]xyz'; $pattern = '/\[url\S*\[\/url\S*\]/'; $str = preg_replace($pattern, '', $str); echo $str . '';
$str = '这是[3]def[25]我的[26]'; $pattern = '/\[([a-z0-9]+)\]/'; preg_match($pattern, $str, $match); print_r($match);
$str = 'abc{title}def{author}mn'; $pattern = '/{(.*)}/U'; preg_match($pattern, $str, $match); print_r($match);
$str = '<img alt="标题" id="pic" src="http://test.cn/avatar.jpg" />'; $pattern = '/<img.*?src="(.*?)".*?\/?>/i'; preg_match($pattern,$str,$match); print_r($match);
$str = '<a href="http://www.test.com/a.html">跳转</a>'; $pattern = '/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/i'; preg_match($pattern, $str, $match); print_r($match);
$str = '12345@qq.com'; $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i"; preg_match($pattern, $str, $match); print_r($match);
$str = '13299803211'; $pattern = "/^1(3|5|8)\d{9}$/"; preg_match($pattern, $str, $match); print_r($match);
$str = '13912345678'; $pattern = '/^139\d{8}$/'; preg_match($pattern,$str,$match); print_r($match);
$str = 'hello 中文 byebye'; $pattern = '/[\x{4e00}-\x{9fa5}]+/u'; preg_match($pattern, $str, $match); print_r($match);
$res = preg_replace($pattern,'',$str); echo $res.’;
$str = '中文字符串'; $res = preg_split('/(?<!^)(?!$)/u', $str); print_r($res);
$str = '<script type="text/javascript" src="dd.js">alert(123);</script><p style="color: red">测试php正则匹配掉js代码</p>'; $pattern = "/<script[\s\S]*?<\/script>/i"; $pattern = "/<[\/\!]*?[^<>]*?>/si"; $res = preg_replace($pattern, "", $str); echo $res;
$str = '<b>abc</b><b>abc</b>'; $pattern = '/<b>(.*?)<\/b>/'; $res = preg_replace($pattern,'\\1',$str); echo $res;
|